如何在XSLT中更改节点的值

4

我希望对一个XML进行一些修改,尝试使用XSLT语句但没有成功。在复制之后,当我尝试应用这些更改时,删除了子节点。以下是需要修改的XML:

`

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

I want to use one xslt to perform the output:

`

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

我尝试了许多模板,但却无法实现。
2个回答

2
您需要更具体地说明您想要完成的任务,才能得到完整的答案,但是希望以下几点提示可以帮助您。使用身份模板,并针对需要删除/更改的特定元素进行覆盖。
要删除origen元素,请使用以下代码:
<xsl:template match="origen"/>

这只是简单地匹配它,并不输出任何内容,有效地将其删除。

要修改元素,您可以使用以下方法:

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

您可以以类似的方式更改其他元素的值。


2

This transformation:

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

应用于提供的XML文档时

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

能够精确地产生期望结果:

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

注意: 为了更系统、更牢固地掌握XSLT,请观看我在Pluralsight上的课程 "XSLT 2.0和1.0基础知识"

另请参阅: https://dev59.com/03RC5IYBdhLWcg3wXPsC#322079


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接