Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt: How to ignore apply-template elements for which there are no matches?

Tags:

xml

xslt

This is my XSL stylesheet:

<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:pp="passcodeProfile">

    <xsl:template match="/">
               <xsl:apply-templates select="elements"/>
    </xsl:template>

    <xsl:template match="element1">
       output1
    </xsl:template> 

    <xsl:template match="element2">
       output2
    </xsl:template> 

</xsl:stylesheet>

And this is the input XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<elements>
    <element1>value1</element1>
    <element2>value2</element2>
    <element3>value3</element3>
    <element4>value4</element4>
</elements>

The current spreadsheet outputs the value of the non-matching elements (outputs element1 element2 value3 value 4). How to ignore elements that are not element1 and element2 (outputs element1 element2)?

like image 494
zer0stimulus Avatar asked Dec 30 '12 14:12

zer0stimulus


1 Answers

Your stylesheet outputs the original text nodes as well. You can suppress them by matching them and doing nothing with them, e.g. add <xsl:template match="text()"/>

like image 124
JanDasWiesel Avatar answered Oct 08 '22 12:10

JanDasWiesel