Aniks
Aniks

Reputation: 1131

XSLT combine two templates filter information

I am new to XSLT and XML.

I am currently filtering some information from an XML document and I want to apply one more template on the same document.

I am not sure how to combine these two templates.

If someone can guide me.

This is my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
<num>0000022222</num>
<first-name>Mike</first-name>
<last-name>Jhonny</last-name>
<licenses>
    <license>
        <number>1762539</number>
    </license>
    <license>
        <number>1762538</number>
    </license>
</licenses>
<not-required>
    <one>12345</one>
    <two>54321</two>
</not-required>
</Person>
<Person>
<num>0000011111</num>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
    <license>
        <number>1762539</number>
    </license>
    <license>
        <number>1762538</number>
    </license>
</licenses>
<not-required>
    <one>12345</one>
    <two>54321</two>
</not-required>
</Person>
</People>

I want to filter the <not-required></not-required> tag and I am able to do it.

I also want to filter the leading zero's for the <num> tag.

I know I can do it by applying this template.

 <xsl:template match="num">
  <xsl:value-of select="number(.)"/>
 </xsl:template>

The XSLT that I am using currently is as follows:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
  <xsl:copy>
  <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="not-required"/>
</xsl:stylesheet>

This is how I am getting the output:

<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
    <number>0000022222</number>
    <first-name>Mike</first-name>
    <last-name>Jhonny</last-name>
    <licenses>
        <license>
            <number>1762539</number>
        </license>
        <license>
            <number>1762538</number>
        </license>
    </licenses>
</Person>
<Person>
    <number>0000011111</number>
    <first-name>John</first-name>
    <last-name>Jhonny</last-name>
    <licenses>
        <license>
            <number>1762539</number>
        </license>
        <license>
            <number>1762538</number>
        </license>
    </licenses>
</Person>
</People>

This is what I am expecting

<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
    <number>22222</number>
    <first-name>Mike</first-name>
    <last-name>Jhonny</last-name>
    <licenses>
        <license>
            <number>1762539</number>
        </license>
        <license>
            <number>1762538</number>
        </license>
    </licenses>
</Person>
<Person>
    <number>11111</number>
    <first-name>John</first-name>
    <last-name>Jhonny</last-name>
    <licenses>
        <license>
            <number>1762539</number>
        </license>
        <license>
            <number>1762538</number>
        </license>
    </licenses>
</Person>
</People>

Can someone guide me how to combine that template to get the leading zeros removed.

Upvotes: 0

Views: 38

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117018

Just add this template to your existing stylesheet (as a third template in addition to the two you already have):

<xsl:template match="num">
    <xsl:copy>
        <xsl:value-of select="number(.)"/>
    </xsl:copy>
</xsl:template>

Upvotes: 2

Related Questions