2011-11-07 6 views
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> 

答えて

9
<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要素)を削除し、それを単に属性に置き換えることです。

解決策は、更新する必要のある属性に一致させることです。

この変換

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

<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文書に適用

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

関連する問題