更改特定属性的值

6

我的需求只是更改“name”属性的值。如果该属性的值为“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>
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接