Raghava
Raghava

Reputation: 5

How to replace xml attributes

Editing attributes of XML through XSLT. this xml document have 10 to 15 attributes of this type.

Example:

<class student="TUTORIAL" Book_type="science" num_pages="250" online_license="yes"  tag="online library"/>

result

<class student="TUTORIAL" Book-type="science" num-pages="250" online-license="yes"  tag="online library"/>

Upvotes: 0

Views: 69

Answers (2)

imran
imran

Reputation: 461

 <xsl:template match="class">
        <xsl:copy>

            <xsl:attribute name="Book-type">
                <xsl:value-of select="@Book_type"/>
            </xsl:attribute>
            <xsl:attribute name="num-pages">
                <xsl:value-of select="@num_pages"/>
            </xsl:attribute>
            <xsl:attribute name="online-license">
                <xsl:value-of select="@online_license"/>
            </xsl:attribute>
            <xsl:apply-templates select="@* except (@Book_type|@num_pages|@online_license)"/>
        </xsl:copy>
    </xsl:template>
Check this code if it is useful for you.

Upvotes: 0

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

First you convert all with identical transformtation

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

after that handle attribute as below

<xsl:template match="@*[contains(name(.), '_')]">
    <xsl:attribute name="{translate(name(), '_', '-')}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

Output

<class student="TUTORIAL" Book-type="science" num-pages="250" online-license="yes" tag="online library"/>

Upvotes: 3

Related Questions