2016-07-08 3 views
1

하나의 XML을 통해 일부 수정을하고 싶습니다. xslt 문장을 사용하려고했지만 변경을 시도 할 때 자식 노드를 복사 한 후 복사 할 수 없습니다.노드 값을 변경하는 방법 xslt

` 

<tree> 
     <sublevel> 
     <definition>subnetting</definition> 
     <status>availabe</status> 
     <categories> 
      <category> 
        <label>IPV4</label> 
        <attributes> 
         <attribute> 
          <label>MASK</label> 
          <value>/24</value> 
         </attribute> 
         <attribute> 
          <label>STARTING_IP</label> 
          <value>10.0.0.1</value> 
         </attribute> 
         <attribute> 
          <label>Type</label> 
          <value>4</value> 
         </attribute> 
        </attributes> 
       </category> 
     </categories> 
     <identifier>subnetworkipv4correct</identifier> 
    </sublevel> 
</tree> 

내가 출력 수행 한 XSLT를 사용하려면 : 은 XML이 게시 한

`

<tree> 
    <sublevel> 
     <definition>subnetting</definition> 
     <status>availabe</status> 
     <origen>template</origen> 
     <categories> 
      <category> 
        <label>IP</label> 
        <attributes> 
         <attribute> 
          <label>Mask</label> 
          <value>/24</value> 
         </attribute> 
         <attribute> 
          <label>START</label> 
          <value>10.0.0.1</value> 
         </attribute> 
         <attribute> 
          <label>Version</label> 
          <value>4</value> 
         </attribute> 
        </attributes> 
       </category> 
     </categories> 
     <identifier>subnetworkipv4correct</identifier> 
     </sublevel> 
    </tree> 

`

내가 가진 테스트 템플릿을 많이하지만 난 그것을 할 수 없다.

답변

0

완전한 대답을 위해 무엇을 하려는지 좀 더 자세하게 설명해야하지만, 몇 가지 지침이 도움이되기를 바랍니다. ID 템플릿을 사용하여 제거/변경이 필요한 특정 요소에 대해 재정의하십시오.

origen 요소를 제거하려면 다음을 사용 :

<xsl:template match="origen"/> 

이 단순히 일치, 아무것도 출력하지를 효과적으로 제거.

label 요소를 수정하려면, 당신은이를 사용할 수 있습니다 : 또는

<xsl:template match="category/label[. = 'IPV4']/text">IP</xsl:template> 

또는를 :

<xsl:template match="category/label/text()"> 
    <xsl:choose> 
    <xsl:when test=". = 'IPV4']">IP</xsl:when> 
    <xsl:otherwise>(default)</xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

당신은 유사한 방법으로 다른 요소의 값을 변경할 수 있습니다.

0

이 변환 :

<tree> 
    <sublevel> 
     <definition>subnetting</definition> 
     <status>availabe</status> 
     <origen>template</origen> 
     <categories> 
     <category> 
      <label>IP</label> 
      <attributes> 
       <attribute> 
        <label>MASK</label> 
        <value>/24</value> 
       </attribute> 
       <attribute> 
        <label>START</label> 
        <value>10.0.0.1</value> 
       </attribute> 
       <attribute> 
        <label>Version</label> 
        <value>4</value> 
       </attribute> 
      </attributes> 
     </category> 
     </categories> 
     <identifier>subnetworkipv4correct</identifier> 
    </sublevel> 
</tree> 
:

<tree> 
    <sublevel> 
     <definition>subnetting</definition> 
     <status>availabe</status> 
     <categories> 
      <category> 
       <label>IPV4</label> 
       <attributes> 
        <attribute> 
         <label>MASK</label> 
         <value>/24</value> 
        </attribute> 
        <attribute> 
         <label>STARTING_IP</label> 
         <value>10.0.0.1</value> 
        </attribute> 
        <attribute> 
         <label>Type</label> 
         <value>4</value> 
        </attribute> 
       </attributes> 
      </category> 
     </categories> 
     <identifier>subnetworkipv4correct</identifier> 
    </sublevel> 
</tree> 

정확하게 원하는 결과을 생산 : 제공된 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()" name="identity"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="status[. = 'availabe']"> 
    <xsl:call-template name="identity"/> 
    <origen>template</origen> 
    </xsl:template> 

    <xsl:template match="label/text()[. = 'IPV4']">IP</xsl:template> 
    <xsl:template match="label/text()[. = 'STARTING_IP']">START</xsl:template> 
    <xsl:template match="label/text()[. = 'Type']">Version</xsl:template> 
</xsl:stylesheet> 

6,

는 : 내 인 Pluralsight 과정을 볼 XSLT에 대한보다 체계적이고 확고한 이해를 얻으려면 "XSLT 2.0 and 1.0 Foundations"또한

참조 : https://stackoverflow.com/a/322079/36305