Search code examples
xsltsequential

XSLT sequential processing


Within class xyz only, I want to examine exactly two divs and their classnames. If classname="yes" then output '1'. If classname="no" then output '0'.

<div class="xyz">
    <div class="no"></div>
    <div class="yes"></div>
</div>

Desired output: 0 1

<div class="xyz">
    <div class="yes"></div>
    <div class="yes"></div>
</div>

Desired output: 1 1

.. etc .. Finding the first is easy but how do I do it "sequentially"?


Solution

  • Recursive processing can be used as in the XSLT-1.0 code below:

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="text"/>
    
        <xsl:template match="div[@class='xyz']/div[@class='no']">
            <xsl:text>0 </xsl:text>
        </xsl:template>
    
        <xsl:template match="div[@class='xyz']/div[@class='yes']">
            <xsl:text>1 </xsl:text>
        </xsl:template>
    
        <xsl:template match="node()">
                <xsl:apply-templates select="node()"/>
        </xsl:template>
    </xsl:transform>
    

    The 3rd template processes all the nodes recursively, starting with document node. The first two templates do the desired output for @class with 'yes' and 'no'.