Gavelarias
Gavelarias

Reputation: 41

how to change node values xslt

I want to have some modificatons over one xml i have try to use xslt sentences but I am unable to do it, after copying I delete the children nodes when I try to put the changes. having the xml posted:

`

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

`

I have test a lot of templates but i am unable to do it.

Upvotes: 4

Views: 9326

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

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>

when applied on the provided XML document:

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

produces exactly the wanted result:

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

Note: To get a more systematic and firm grasp of XSLT, watch my Pluralsight course "XSLT 2.0 and 1.0 Foundations"

See also: https://stackoverflow.com/a/322079/36305

Upvotes: 2

Flynn1179
Flynn1179

Reputation: 12075

You'd need to be a little more precise about what you're trying to do for a complete answer, but hopefully a few pointers should help. Use the identity template, and override it for specific elements that need removing/changing.

To remove the origen element, use this:

<xsl:template match="origen"/>

This simply matches it, and outputs nothing, effectively removing it.

To modify the label element, you can use this:

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

Or alternatively:

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

You can change values of other elements in a similar way.

Upvotes: 2

Related Questions