使用模板复制时如何在 XSLT 中创建元素

2024-04-08

我正在尝试在 XML 中创建一个元素,其中复制和修改了基本内容。

我的 XML 是这样的

<root>
  <node>
     <child>value</child>
     <child2>value2</child2>
  </node>
  <node2>bla</node2>
</root>

节点的子节点数量以及根的子节点数量可能会发生变化。 XSLT应该复制整个内容,修改一些值and添加一些新的。

复制修改没问题:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy> 
  </xsl:template>
</xsl:stylesheet>

(+更多修改模板)。

但是如何在某个路径上的该结构中添加新元素,例如我想添加一个元素作为“node”节点的最后一个元素。 “节点”元素本身始终存在。


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" encoding="UTF-8"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy> 
  </xsl:template>
  <xsl:template match="node">
    <node>
      <xsl:apply-templates select="@*|node()"/>
      <newNode/>
    </node> 
  </xsl:template>
</xsl:stylesheet>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用模板复制时如何在 XSLT 中创建元素 的相关文章

随机推荐