Change the value of a specific attribute

2019-04-28 10:48发布

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>

标签: xml xslt
1条回答
该账号已被封号
2楼-- · 2019-04-28 11:20
   <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>
查看更多
登录 后发表回答