xml – 更改特定属性的值

前端之家收集整理的这篇文章主要介绍了xml – 更改特定属性的值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的需要只是重新获得“名称”的属性值.如果此属性的值为“default”,则应将其更改为“New”.休息一切都应该是输入xml的副本.我尝试使用下面的xsl然而它无法正常工作.
<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>

输入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>
<xsl:template match="SORRegion[@name='default']"> 
    <xsl:attribute name="name"> 
      <xsl:value-of select="'New'"/> 
    </xsl:attribute>   
    <xsl:copy-of select="child::*"/> 
   </xsl:template>

代码存在许多问题.最重要的问题是它删除当前节点(SORRegion元素)并仅用属性替换它.

解决方案是匹配应更新的属性.

这种转变:

<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>

当应用于提供的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>

产生完全想要的,正确的结果:

<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>

猜你在找的XML相关文章