How can I make an exact copy of a xml node's c

2019-07-03 14:39发布

问题:

My problem is that my XML document contains snippets of XHTML within it and while passing it through an XSLT I would like it to render those snippets without mangling them.

I've tried wrapping the snippet in a CDATA but it doesn't work since less than and greater than are translated to < and > as opposed to being echoed directly.

What's the XSL required for doing this?

回答1:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

This is referred to as the "identity transformation" in the XSLT specification.



回答2:

I ran in that problem and the copy-of is certainly the easiest to use. The identity works, but that's 5 lines of code and you'd need to call such a template, not just define it as is in your XSLT document (otherwise you probably won't get what you expected in your output.)

My main problem actually was to copy the content of a tag, and not the tag itself. It's actually very easy to resolve but it took me a little time to figure it out (maybe because QtXmlPatterns crashes quite a bit!)

So, the following copies the tag named here and all of its children:

<xsl:copy-of select="this/tag/here"/>

But most often you do not want to do that because <here> is actually the container, in other words, it should not appear in the output. In that case you can simply do this:

<xsl:copy-of select="this/tag/here/*"/>

This copies all the children found in the tag named <here>.



回答3:

Assuming your xhtml is in an element YYY

http://www.dpawson.co.uk/xsl/sect2/N1930.html explains options



回答4:

xsl:copy-of



标签: xml xslt xhtml