Gnaneshwar
Gnaneshwar

Reputation: 15

Need to use for each in choose in xslt

I have a requirement where I need to use for each only for when condition in choose in xslt.

Input:`<input>baz</input>
<input>haz</input>
<input>nan</input>`

XSLT:

`<xsl:choose> 
<xsl:for-each select="input">
<xsl:when test="input='haz'">
<output>true</output>
</xsl:when>
</xsl:for-each>
<xsl:otherwise>
<output>false</output>
</xsl:otherwise>`

any help is really appreciated.

Upvotes: 0

Views: 1007

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

Given a well-formed input such as:

XML

<root>
    <input>baz</input>
    <input>haz</input>
    <input>nan</input>
</root>

the following stylesheet:

XSLT 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:template match="/root">
    <output>
        <xsl:choose> 
            <xsl:when test="input='haz'">true</xsl:when>
            <xsl:otherwise>false</xsl:otherwise>
        </xsl:choose> 
    </output>
</xsl:template> 

</xsl:stylesheet>

will return:

<?xml version="1.0" encoding="UTF-8"?>
<output>true</output>

P.S.

If you like, you could shorten the stylesheet to just:

<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:template match="/root">
    <output>
        <xsl:value-of select="input='haz'"/>
    </output>
</xsl:template> 

</xsl:stylesheet>

Upvotes: 1

Related Questions