Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple xsl:choose within a single xsl:when

Tags:

xml

xslt

If I am testing several different defined parameters can I call multiple xsl:when statements within on xsl:choose statement? So if I have:

<parameters>
                    <param id="1">
                        <key>Load</key>
                        <value>XML</value>
                    </param>
                    <param id="2">
                        <key>Input</key>
                        <value>Http</value>
                    </param>
                    <param id="3">
                        <key>Response</key>
                        <value>Y</value>
                    </param> 
</parameters> 

could I call three different <xsl:when> with a single <xsl:choose> to check the because I will have several <param> with different <value> that will later need to help call different templates.

like image 819
gary A.K.A. G4 Avatar asked Jun 30 '10 15:06

gary A.K.A. G4


People also ask

Which of the following elements can be used along with xsl choose to specify a multiple conditional test against the content of nodes?

The XSLT <xsl:choose> element is used to specify a multiple conditional test against the content of nodes with the <xsl:otherwise> and <xsl:when> elements.

How do I pass multiple parameters in XSLT?

In order to pass multiple parameters to XSLT from BP you'll need to pass '=' as a separator.

What are the two instructions in xsl that allow you to conditionally process an element based on specific test conditions?

Almost all programming languages have conditional branching structures. XSL has two: <xsl:if> and <xsl:choose>. XPath also has the if-then-else structure.


2 Answers

Yes, sure, you can. You can also add an xsl:otherwise node to handle all other cases:

<xsl:choose>
    <xsl:when test="param/@id = '1'">
        <xsl:text>XML</xsl:text>
    </xsl:when>
    <xsl:when test="param/@id = '2'">
        <xsl:text>HTTP</xsl:text>
    </xsl:when>
    <xsl:when test="param/@id = '3'">
        <xsl:text>Y</xsl:text>
    </xsl:when>
    <xsl:otherwise>
        <xsl:text>Other format</xsl:text> 
    </xsl:otherwise>
</xsl:choose>

This is similar to the switch statement in C-like languages and Java, or the Select Case in VB.

Of course, you can check for arbitrary XPath expressions, e.g. you could check as well for

<xsl:when test="param/key = 'Input'">
    <!-- handle input -->
</xsl:when>
like image 120
Dirk Vollmar Avatar answered Oct 12 '22 16:10

Dirk Vollmar


could I call three different with a single to check the because I will have several with different that will later need to help call different templates.

Yes, you can and @0xA3 showed you how.

But you needn't and shouldn't!

It is in the spirit of XSLT that the XSLT processor can decide what template to apply on what condition.

This transformation:

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

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

 <xsl:template match="param[@id=1]">
    Load!
 </xsl:template>

 <xsl:template match="param[@id=2]">
    Input!
 </xsl:template>

 <xsl:template match="param[@id=3]">
    Response!
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<parameters>
    <param id="1">
        <key>Load</key>
        <value>XML</value>
    </param>
    <param id="2">
        <key>Input</key>
        <value>Http</value>
    </param>
    <param id="3">
        <key>Response</key>
        <value>Y</value>
    </param>
</parameters>

produces:

<parameters>
    Load!

    Input!

    Response!
 </parameters>

Do note that we didn't use any conditional logic -- the XSLT processor itself decided which template to apply for the <param> elements with different id attributes.

This is a great facility(in this concrete case you saved 14 lines of code that is error-prone) and is recommended to be used as often as possible

like image 31
Dimitre Novatchev Avatar answered Oct 12 '22 15:10

Dimitre Novatchev