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

2019-07-03 14:49发布

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?

标签: xml xslt xhtml
4条回答
地球回转人心会变
2楼-- · 2019-07-03 15:27

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楼-- · 2019-07-03 15:31
<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.

查看更多
放我归山
4楼-- · 2019-07-03 15:32

Assuming your xhtml is in an element YYY

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

查看更多
Ridiculous、
5楼-- · 2019-07-03 15:32

xsl:copy-of

查看更多
登录 后发表回答