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

∥☆過路亽.° 提交于 2019-12-25 02:26:40

问题


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...


回答1:


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')



回答2:


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.



来源:https://stackoverflow.com/questions/5815628/convert-copy-of-output-to-string-and-escape-xml-special-characters-like-less-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!