Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put correct attribute '@id' value based on the '@rid' value

I’m trying to put correct value in the attribute @id based on the @rid attribute value.
Logic: If element a more then 1 and element cu equal to 1 in the ‘au’ element than how to add position after the attribute value e.g (<a id="a">...</a>, <a id="a">...</a>) s/b (<a id="a1">...</a>, <a id="a2">...</a>)
How to solve this issue template match only a element.
Please check Expecting Output:

Input:

<root>
<t>Title</t>
<au>
    <cu>
        <x rid="a1 a2"/>
    </cu>
</au>
<a id="a">Centre</a>
<a id="a">Westmead</a>
<n>notes</n>
</root>

XSLT Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

<xsl:template match="a">
    <xsl:if test="count(../a) &gt; 1 and count(../au/cu[x]) = 1 and @id">
        <xsl:for-each-group select="self::node()" group-adjacent="self::a">
                <xsl:variable name="pos" select="position()"/>
                <xsl:element name="a">
                    <xsl:attribute name="id">
                        <xsl:value-of select="concat('a',$pos)"/>
                    </xsl:attribute>
                    <xsl:apply-templates/>
                </xsl:element>
            <!--</xsl:for-each>-->
        </xsl:for-each-group>
    </xsl:if>
</xsl:template>

Expecting Output:

<root>
<t>Title</t>
<au>
    <cu>
        <x rid="a1 a2"/>
    </cu>
</au>
<a id="a1">Centre</a>
<a id="a2">Westmead</a>
<n>notes</n>

>Solution :

If you want to do it from the template for a use

  <xsl:template match="root[a[2] and not(au/cu[x][2])]/a[@id]">
    <xsl:copy>
      <xsl:attribute name="id">a<xsl:number/></xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading