How can I make an exact copy of a xml node's children with XSLT?

大城市里の小女人 提交于 2019-12-12 12:36:12

问题


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



来源:https://stackoverflow.com/questions/56837/how-can-i-make-an-exact-copy-of-a-xml-nodes-children-with-xslt

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