2015-11-04 52 views
0

我有一个XML:XSLT替换属性值是重复的父节点的

<?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> 

我想找出节点TAG2具有价值作为价值父节点TAG1的ID相同的ID,更换tag2 id的值为原始ID,后缀为“D”。

在上面的例子: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> 

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

回答

1

使用模板

<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