How to implement “if else if else” condition in XSLT

后端 未结 7 1981
心在旅途
心在旅途 2021-01-07 18:36

Is it possible to implement \"if else, if else\" in xsl? for example I have data:


    
        MAR111
          


        
相关标签:
7条回答
  • 2021-01-07 18:57

    Do you mean something like:

    <xsl:choose>
      <xsl:when test="name() = 'MAR111'">
        ... do something ...
      </xsl:when>
      <xsl:otherwise>
        ... do something as fallback ...
      </xsl:otherwise>
    </xsl:choose>
    

    BR Markus

    0 讨论(0)
  • 2021-01-07 18:58

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:key name="kLineByName" match="line" use="name"/>
        <xsl:template match="line[count(.|key('kLineByName',name)[1]) = 1]">
            <document>
                <xsl:element name="{substring(name,1,3)}">
                    <xsl:copy-of select="name|key('kLineByName',name)/value"/>
                </xsl:element>
            </document>
        </xsl:template>
        <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    Output:

    <document>
        <MAR>
            <name>MAR111</name>
            <value>1</value>
            <value>3</value>
        </MAR>
    </document>
    <document>
        <MEA>
            <name>MEA111</name>
            <value>1</value>
            <value>4</value>
        </MEA>
    </document>
    <document>
        <MPR>
            <name>MPR111</name>
            <value>1</value>
            <value>2</value>
        </MPR>
    </document>
    
    0 讨论(0)
  • 2021-01-07 19:01

    The best way is to use separate templates.

    <xsl:template match="/document/line/name='MEA111'">
           <xsl:apply-templates mode="MEA" select="/document"/>
    </xsl:template>
    
    <xsl:template match="/document/line/name='MPR111'">
           <xsl:apply-templates mode="MPR" select="/document"/>
    </xsl:template>
    
    <xsl:template match="/document/line/name='MAR111'">
           <xsl:apply-templates mode="MAR" select="/document"/>
    </xsl:template>
    

    Even less lines and this is more maintainable.

    0 讨论(0)
  • 2021-01-07 19:05

    Yes, we can achieve things using <xsl:choose><xsl:when> but if, else if, condition is also supported in @select attribute of different xslt construct for example :

    <xsl:value-of select="if (@geography = 'North America') then 
                     'Domestic car'
                    else if (@geography = 'Europe') then 
                      'Import from Europe'
                    else if (@geography = 'Asia') then 
                      &quot;It's from Asia&quot;
                    (: If it's anything else :)
                    else 
                       'We don''t know!'"/>
    
    0 讨论(0)
  • 2021-01-07 19:17

    No, choose when is the xsl way of saying if else. No better way

    0 讨论(0)
  • 2021-01-07 19:17

    if you want to implement a catch-all fall through (e.g. equivalent to "else"), you should use otherwise

    0 讨论(0)
提交回复
热议问题