Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 109

XSLT modify attribute value

Let's say we have this source XML:

<?xml version="1.0" encoding="UTF-8"?>
<SyncSupplierPartyMaster xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.10.x/InforOAGIS/BODs/Developer/SyncSupplierPartyMaster.xsd" releaseID="9.2" versionID="2.10.x">
    <DataArea>
        <SupplierPartyMaster>
            <Classification>
                <Codes>
                    <Code listID="CLASS" sequence="1">3</Code>
                    <Code listID="CURRENCY" sequence="2">EUR</Code>
                    <Code listID="KVK" sequence="3">-</Code>
                </Codes>
            </Classification>
        </SupplierPartyMaster>
    </DataArea>
</SyncSupplierPartyMaster>

I need to change CLASS to MROClass. I have this XSL, which does not work, can someone please advise what is wrong? The transformation can be also found on: http://xsltransform.net/gWEamL8/1

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schema.infor.com/InforOAGIS/2">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />   

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

<!-- Rename attribute Class to MROClass --> 


<xsl:template match="@ListID[.='CLASS']">
    <xsl:attribute name="ListID">
        <xsl:value-of select="'MROClass'"/>
    </xsl:attribute>
 </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Views: 3222

Answers (1)

Tim C
Tim C

Reputation: 70598

XSLT is case-sensitive. Your XML attributes and listID, but your XSLT is looking for ListID (with an upper-case L). Try changing the template to this:

<xsl:template match="@listID[.='CLASS']">
    <xsl:attribute name="listID">
        <xsl:value-of select="'MROClass'"/>
    </xsl:attribute>
 </xsl:template>

Note that you don't really need to output text with xsl:value-of, you could do this...

<xsl:template match="@listID[.='CLASS']">
    <xsl:attribute name="listID">
       <xsl:text>MROClass</xsl:text>
     </xsl:attribute>
 </xsl:template>

Or even just this:

<xsl:template match="@listID[.='CLASS']">
   <xsl:attribute name="listID">MROClass</xsl:attribute>
</xsl:template>

Upvotes: 2

Related Questions