anon
anon

Reputation:

Change the value of a specific attribute

My need is just to repalce the attribute value of "name". If this attribute has "default" as the value, It should be changed to "New". Rest everything should be copy of the input xml. i tried with the below xsl however it is not working.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

   <xsl:template match="SORRegion[@name='default']">
    <xsl:attribute name="name">
    <xsl:value-of select="'New'"/>
    </xsl:attribute>  
     <xsl:copy-of select="child::*"/>
    </xsl:template>

</xsl:stylesheet>

Input xml

<RoutingDetails>
    <Service ServiceName="StatementIndicatorsService">
        <SOR SORname="Globestar">
            <CountryCode Ctrycd="124">
                <SORRegion name="Test">
                    <ConsumerName name="MYCA">
                        <AutomationIds>
                            <PreAutoId>
                                <AutomationId>XA1146A</AutomationId>
                                <AutomationId>XA1146A</AutomationId>
                            </PreAutoId>
                            <DefaultAutoId>
                                <AutomationId>XA1146A</AutomationId>
                            </DefaultAutoId>
                        </AutomationIds>
                    </ConsumerName>
                    <QueueDetails>
                        <QueueManager>MAO1</QueueManager>
                        <ReplyQueueManager>MAO1</ReplyQueueManager>
                        <RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
                        <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
                    </QueueDetails>

                </SORRegion>
                <SORRegion name="default">
                    <ConsumerName name="MYCA">
                        <AutomationIds>
                            <PreAutoId>
                                <AutomationId>XA1146A</AutomationId>
                                <AutomationId>XA1146A</AutomationId>
                            </PreAutoId>
                            <DefaultAutoId>
                                <AutomationId>XA1146A</AutomationId>
                            </DefaultAutoId>
                        </AutomationIds>
                    </ConsumerName>
                    <QueueDetails>
                        <QueueManager>MAO1</QueueManager>
                        <ReplyQueueManager>MAO1</ReplyQueueManager>
                        <RequestQueue>DEFAULT.REQUEST</RequestQueue>
                        <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
                    </QueueDetails>

                </SORRegion>
            </CountryCode>
        </SOR>
    </Service>
</RoutingDetails>

Upvotes: 6

Views: 10239

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

   <xsl:template match="SORRegion[@name='default']"> 
    <xsl:attribute name="name"> 
      <xsl:value-of select="'New'"/> 
    </xsl:attribute>   
    <xsl:copy-of select="child::*"/> 
   </xsl:template> 

There are a number of problems with this code. The most important problem is that it deletes the current node (the SORRegion element) and replaces it with just an attribute.

The solution is to match the attribute that should be updated.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="SORRegion/@name[.='default']">
  <xsl:attribute name="name">
    <xsl:value-of select="'New'"/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<RoutingDetails>
    <Service ServiceName="StatementIndicatorsService">
        <SOR SORname="Globestar">
            <CountryCode Ctrycd="124">
                <SORRegion name="Test">
                    <ConsumerName name="MYCA">
                        <AutomationIds>
                            <PreAutoId>
                                <AutomationId>XA1146A</AutomationId>
                                <AutomationId>XA1146A</AutomationId>
                            </PreAutoId>
                            <DefaultAutoId>
                                <AutomationId>XA1146A</AutomationId>
                            </DefaultAutoId>
                        </AutomationIds>
                    </ConsumerName>
                    <QueueDetails>
                        <QueueManager>MAO1</QueueManager>
                        <ReplyQueueManager>MAO1</ReplyQueueManager>
                        <RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
                        <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
                    </QueueDetails>

                </SORRegion>
                <SORRegion name="default">
                    <ConsumerName name="MYCA">
                        <AutomationIds>
                            <PreAutoId>
                                <AutomationId>XA1146A</AutomationId>
                                <AutomationId>XA1146A</AutomationId>
                            </PreAutoId>
                            <DefaultAutoId>
                                <AutomationId>XA1146A</AutomationId>
                            </DefaultAutoId>
                        </AutomationIds>
                    </ConsumerName>
                    <QueueDetails>
                        <QueueManager>MAO1</QueueManager>
                        <ReplyQueueManager>MAO1</ReplyQueueManager>
                        <RequestQueue>DEFAULT.REQUEST</RequestQueue>
                        <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
                    </QueueDetails>

                </SORRegion>
            </CountryCode>
        </SOR>
    </Service>
</RoutingDetails>

produces exactly the wanted, correct result:

<RoutingDetails>
   <Service ServiceName="StatementIndicatorsService">
      <SOR SORname="Globestar">
         <CountryCode Ctrycd="124">
            <SORRegion name="Test">
               <ConsumerName name="MYCA">
                  <AutomationIds>
                     <PreAutoId>
                        <AutomationId>XA1146A</AutomationId>
                        <AutomationId>XA1146A</AutomationId>
                     </PreAutoId>
                     <DefaultAutoId>
                        <AutomationId>XA1146A</AutomationId>
                     </DefaultAutoId>
                  </AutomationIds>
               </ConsumerName>
               <QueueDetails>
                  <QueueManager>MAO1</QueueManager>
                  <ReplyQueueManager>MAO1</ReplyQueueManager>
                  <RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
                  <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
               </QueueDetails>
            </SORRegion>
            <SORRegion name="New">
               <ConsumerName name="MYCA">
                  <AutomationIds>
                     <PreAutoId>
                        <AutomationId>XA1146A</AutomationId>
                        <AutomationId>XA1146A</AutomationId>
                     </PreAutoId>
                     <DefaultAutoId>
                        <AutomationId>XA1146A</AutomationId>
                     </DefaultAutoId>
                  </AutomationIds>
               </ConsumerName>
               <QueueDetails>
                  <QueueManager>MAO1</QueueManager>
                  <ReplyQueueManager>MAO1</ReplyQueueManager>
                  <RequestQueue>DEFAULT.REQUEST</RequestQueue>
                  <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
               </QueueDetails>
            </SORRegion>
         </CountryCode>
      </SOR>
   </Service>
</RoutingDetails>

Upvotes: 10

Related Questions