xslt 替换父节点重复的属性值

xslt replace attribute values that are duplicated of parent node

我有一个 XML:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
    <tag1 id="abc" name="first tag">
        <tag2 id="efg" name="embedded tag1">
        </tag2>
    </tag1>
    <tag1 id="hij" name="first tag">
        <tag2 id="hij" name="embedded tag1">
        </tag2>
    </tag1>
    <LOTS OF TAG1/TAG2S>...</>
</ROOT>

我想找出与父节点tag1的id值相同的节点tag2,将tag2的id值替换为后缀为"D"的原始id。

在上面的示例中:tag2 的 id "hij" 与其父节点 tag1 相同,因此应将其替换为:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
    <tag1 id="abc" name="first tag">
        <tag2 id="efg" name="embedded tag1">
        </tag2>
    </tag1>
    <tag1 id="hij" name="first tag">
        <tag2 id="hijD" name="embedded tag1">
        </tag2>
    </tag1>
    <LOTS OF TAG1/TAG2S>...</>
</ROOT>

我写了一个xslt:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="//tag2/@id[.=../tag1/@id]">
  <xsl:with-param name="id" select="@id" />
  <xsl:with-param name="extra" select="'D'" />
  <xsl:attribute name="id">
    <xsl:value-of select="concat($id,$extra)"/>
  </xsl:attribute>
</xsl:template>

没有按预期工作。 任何灯将不胜感激!

使用模板

<xsl:template match="tag2/@id[.=../../@id]">
    <xsl:attribute name="{name()}">
        <xsl:value-of select="concat(., 'D')"/>
    </xsl:attribute>
</xsl:template>

您需要 .. 两次,因为第一个 .. 选择 tag2 元素,第二个 tag1 元素。

样本位于 http://xsltransform.net/bFN1y8Y