0

I have a 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>

I would like to find out the nodes tag2 which have the same id value as the parent node tag1's id value, replace the value of tag2 id to original id with suffix "D".

In above example: tag2 has id "hij" which is the same as its parent node tag1, hence it should be replaced as:

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

I wrote an 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>

Doesn't quite work as expected. Any lights would be appreciated!

1 Answer 1

1

Use the template

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

You need .. twice as the first .. selects the tag2 element and the second the tag1 element.

A sample is at http://xsltransform.net/bFN1y8Y.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.