2011-11-07 72 views
6

Nhu cầu của tôi chỉ là để trả giá trị thuộc tính của "tên". Nếu thuộc tính này có "mặc định" làm giá trị, nó phải được thay đổi thành "Mới". Phần còn lại mọi thứ nên là bản sao của xml đầu vào. tôi đã thử với các xsl dưới đây tuy nhiên nó không hoạt động.Thay đổi giá trị của một thuộc tính cụ thể

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

Trả lời

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> 

Có một số vấn đề với mã này. Vấn đề quan trọng nhất là nó xóa nút hiện tại (phần tử SORRegion) và thay thế nó chỉ với một thuộc tính.

Giải pháp là khớp với thuộc tính cần được cập nhật.

chuyển đổi này:

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

khi áp dụng trên tài liệu XML cung cấp:

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

sản xuất chính xác mong muốn, đúng kết quả:

<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> 
Các vấn đề liên quan