简体   繁体   中英

XSLT - copy attribute by comparing other element attribute

I have a sample xml like this,

<doc>
    <aa type="aaa" id="ggg">text</aa>
    <aa type="bbb" id="hhh">text</aa>
    <aa type="ccc" id="iii">text</aa>
    <aa type="ccc" id="jjj">text</aa>
    <aa type="bbb" id="kkk">text</aa>
    <aa type="aaa" id="lll">text</aa>
</doc>

As you can see there are 2 elements exist with equal type attribute here and what I need is interchange the id attribute values if elements where type attribute is equal.

so, for above example, output should be,

<doc>
    <aa type="aaa" id="lll">text</aa>
    <aa type="bbb" id="kkk">text</aa>
    <aa type="ccc" id="jjj">text</aa>
    <aa type="ccc" id="iii">text</aa>
    <aa type="bbb" id="hhh">text</aa>
    <aa type="aaa" id="ggg">text</aa>
</doc>

I've written following xsl to do this,

<xsl:template match="aa[@type='aaa' or @type='bbb' or @type='ccc'][1]">
        <xsl:copy>
            <xsl:if test="following::aa[@type=self::node()/@type]">
                <xsl:attribute name="id">
                    <xsl:value-of select="following::aa[@type=self::node()/@type]/@type"/>
                </xsl:attribute>
            </xsl:if>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="aa[@type='aaa' or @type='bbb' or @type='ccc'][2]">
        <xsl:copy>
            <xsl:if test="following::aa[@type=self::node()/@type]">
                <xsl:attribute name="id">
                    <xsl:value-of select="preceding::aa[@type=self::node()/@type]/@type"/>
                </xsl:attribute>
            </xsl:if>
        </xsl:copy>
    </xsl:template>

but this doesn't as expected, any anyone suggest me a method how can I do this using XSLT?

try this one

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes" omit-xml-declaration="yes"/>

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

    <xsl:template match="aa">
        <xsl:variable name="type" select="@type"/>
        <xsl:copy>
            <xsl:apply-templates select="@type"/>
            <xsl:choose>
                <xsl:when test="following::aa[@type=$type]">
                    <xsl:attribute name="id">
                        <xsl:value-of select="following::aa[@type=$type]/@id"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="preceding::aa[@type=$type]">
                    <xsl:attribute name="id">
                        <xsl:value-of select="preceding::aa[@type=$type]/@id"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM