2011-11-07 37 views
6

La mia necessità è solo di ripetere il valore dell'attributo di "nome". Se questo attributo ha valore "predefinito" come valore, dovrebbe essere cambiato in "Nuovo". Resto tutto dovrebbe essere copia dell'ingresso xml. ho provato con il sotto xsl tuttavia non funziona.Modificare il valore di un attributo specifico

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

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

risposta

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> 

ci sono una serie di problemi con questo codice. Il problema più importante è che elimina il nodo corrente (l'elemento SORRegion) e lo sostituisce con un solo attributo.

La soluzione deve corrispondere all'attributo che deve essere aggiornato.

Questa trasformazione:

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

quando applicato sul documento XML fornito:

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

produce esattamente la desiderata, risultato corretto:

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