使用 XSLT 复制 XML 中的所有节点,支持特殊情况 [英] Using XSLT to copy all nodes in XML, with support for special cases
本文介绍了使用 XSLT 复制 XML 中的所有节点,支持特殊情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个大型 XML 文件,其结构如下:
Say I have a large XML file that the following structure:
<MyXml>
<Data1>
<Node1>1234</Node1>
<Node2>abc<Node2>
<Node3>gfdf</Node3>
...
<Node10000>more text</Node10000>
</Data1>
<Data2>
...
</Data2>
</MyXml>
我想将此 XML 转换为另一个看起来完全相同的 XML,但将某个字符串连接到某个节点,例如 Node766.我当然在使用 XSLT,想知道如何告诉它按原样复制所有内容,除了 Node766,在输出之前我必须做一些事情.
I want to transform this XML into another XML that looks exactly the same, but has a certain string concatinated to a certain node, say Node766. I am using an XSLT of course and wondering how I can tell it to copy everyhing as-is except for Node766, where I have to do something before outputing it.
推荐答案
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--Identity template,
provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--More specific template for Node766 that provides custom behavior -->
<xsl:template match="Node766">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<!--Do something special for Node766, like add a certain string-->
<xsl:text> add some text </xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这篇关于使用 XSLT 复制 XML 中的所有节点,支持特殊情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文