使用 xslt 在父节点中创建标签

use xslt to create tag in in parent node

假设我有一个类似这样的 XML 文档(请注意,就我的目的而言,标签在最终文档中出现的顺序无关紧要,以防万一;我们只是将 XML 用作奇特的 key/value 配对装置)

<MyRoot>
 various tags here I don't care about at the moment
 <child><GoldenTag>1234</GoldenTag></child>
</MyRoot>

注意GoldenTag的值并不总是1234;这只是为了说明。现在,我想创建一个 SilverTag,它是 MyRoot 的(直接)子代,其值与 GoldenTag 相同,假设 GoldenTag 存在并且满足各种其他条件。

我看不出这样做的好方法,因为如果我使用 template mach='GoldenTag',那么模板的内容最终会出现在 child 标签内,这不是我想要的位置。

MyRoot 上的任何匹配当然也可以包括其他元素的条件,例如

<xsl:template match="MyRoot[child/GoldenTag and your-other-conditions]">
  <xsl:copy>
      <xsl:apply-templates/>
      <SilverTag><xsl:value-of select="child/GoldenTag"/></SilverTag>
  </xsl:copy>
</xsl:template>

根据 Martin 的回答,我认为(希望?)这样的方法应该有效:

<xsl:template match="child[GoldenTag and other-tests]">
    <xsl:copy-of select="."/>
    <SilverTag>
        <xsl:value-of select="GoldenTag"/>
    </SilverTag>
</xsl:template>