正如其他答案所指出的,XPath 无法修改 XML 文档并生成新节点。
由于"set" 的定义,任何节点只能参与一次节点集。
但是,XPath 2.0 为我们提供了新的sequence type,,它允许重复项目。
为了让一个元素在一个序列中出现两次,只需使用the sequence concatenation operator ",",如下所示:
/*/ElementYouWant, /*/ElementYouWant
像这样简单地将其放入 XSLT2.0 样式表中:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f xs"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<t>
<xsl:sequence select=
"/*/ElementYouWant, /*/ElementYouWant"/>
</t>
</xsl:template>
</xsl:stylesheet>
并将此转换应用于此 XML 文档:
<toplevel>
<ElementYouWant>
<SomeSubElement1>specific data</SomeSubElement1>
<SomeSubElement2>specific data 2</SomeSubElement2>
</ElementYouWant>
</toplevel>
产生想要的结果:
<t>
<ElementYouWant>
<SomeSubElement1>specific data</SomeSubElement1>
<SomeSubElement2>specific data 2</SomeSubElement2>
</ElementYouWant>
<ElementYouWant>
<SomeSubElement1>specific data</SomeSubElement1>
<SomeSubElement2>specific data 2</SomeSubElement2>
</ElementYouWant>
</t>
请注意,如果使用<xsl:sequence> 指令,则不会创建<ElementYouWant> 元素的新副本——因此在XSLT 2.0 中建议使用<xsl:sequence> 并避免使用<xsl:copy-of>,它会创建(不必要的)节点副本。