2011-11-07 3 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> 

관련 문제