gary A.K.A. G4
gary A.K.A. G4

Reputation: 1053

nested <xsl:if> & <xsl:when>

Can you nest xsl:if within nested xsl:when, for example:

    <xsl:choose>
        <xsl:when test="param/@id  =  '1' " >
            <xsl:if test="param/value = 'XML' ">
                <xsl:when test="param/@id = '2' ">
                    <xsl:if test="param/value = 'HTTP' ">
                        <xsl:when test="param/@id = '3' ">
                            <xsl:if test="param/value = 'Y' ">
                                <xsl:call-template name="buildPayload"/>
                            </xsl:if>
                        </xsl:when>
                    </xsl:if>
                </xsl:when>
            </xsl:if>
        </xsl:when>
    </xsl:choose>

Can this be used, or is there away to streamline this into a more compact code?

Upvotes: 2

Views: 14552

Answers (3)

orithena
orithena

Reputation: 1485

Without trying it: I'd think that <xsl:when> cannot be a child node of <xsl:if> without another <xsl:choose> in between. But, what are you trying to do? If I see that right, you want to run <call-template> if and only if (param/@id='1' and param/value='XML') or (param/@id='2' and param/value='HTTP') or (param/@id='3' and param/value='Y') ... try that as the test value:

<xsl:if test="(param/@id='1' and param/value='XML') or (param/@id='2' and param/value='HTTP') or (param/@id='3' and param/value='Y')">
    <xsl:call-template name="buildPayload"/>
</xsl:if>

Upvotes: 7

Matthew Whited
Matthew Whited

Reputation: 22443

You may want to read up on XPath. You should be able to use XPath Expressions and the Node Selection syntax to help with this selection.

You may also try to use a visual XSLT designer such as the tooling in Visual Studio. Visual Studio, for example, will provide IntelliSense and validation for XSLT.

Upvotes: 0

Kramer
Kramer

Reputation: 148

Yeah this looks like it will not work. You could add "and"s and "or"s in the test for the when.

Upvotes: 1

Related Questions