Convert copy-of output to string and escape XML special characters (like less than (<) and greater than (>) symbols)

StackOverflow https://stackoverflow.com/questions/5815628

  •  26-10-2019
  •  | 
  •  

سؤال

I am trying this in an XQuery (assume that doc('input:instance') does indeed return a valid XML document) which is generated using XSLT

let $a:= <xsl:text>"<xsl:copy-of select="doc('input:instance')//A" />"</xsl:text>
let $p := <xsl:text>"<xsl:copy-of select="doc('input:instance')//P" />"</xsl:text>
let $r := <xsl:text>"<xsl:copy-of select="doc('input:instance')//R" />"</xsl:text>

But I get the error:

xsl:text must not contain child elements

How do I retrieve XML results using the XPath in xsl:copy-of and then encode the special characters received in the result while formatting the result as string? I would be happy to use CDATA section if that's possible (if I do that instead of xsl:text above, xsl:copy-of is not evaluated since it becomes part of CDATA section).

Obviously I am a newcomer to XSL...

هل كانت مفيدة؟

المحلول

What you need here is the ability to serialize an XML document (here the document returned by doc()) using the XML serialization, into a string.

Various XQuery implementation have extension functions for this purpose. For example, if you are using Saxon:

saxon:serialize(document, 'xml')

نصائح أخرى

This has nothing to do with XQuery (you could be building the XSLT stylesheet with any language, even XSLT itslef!).

From http://www.w3.org/TR/xslt20/#xsl-text

<!-- Category: instruction -->
<xsl:text
  [disable-output-escaping]? = "yes" | "no">
  <!-- Content: #PCDATA -->
</xsl:text>

[...] The content of the xsl:text element is a single text node whose value forms the string value of the new text node.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top