开发者

Multiple xsl:choose within a single xsl:when

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</v开发者_如何转开发alue>
                    </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.


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜