Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement "if else if else" condition in XSLT

Tags:

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

<document>     <line>         <name>MAR111</name>         <value>1</value>     </line>      <line>         <name>MAR111</name>         <value>3</value>     </line>     <line>         <name>MEA111</name>         <value>1</value>     </line>     <line>         <name>MPR111</name>         <value>1</value>     </line>     <line>         <name>MEA111</name>         <value>4</value>     </line>     <line>         <name>MPR111</name>         <value>2</value>     </line> </document> 

I need to get three document templates with three names:

<document>     <MAR>         <name>MAR111</name>         <number>1</number>         <number>4</number>     </MAR> </document> <document>     <MEA>         <name>MEA111</name>         <number>1</number>         <number>4</number>     </MEA> </document> <document>     <MPR>         <name>MPR111</name>         <number>1</number>         <number>2</number>     </MPR> </document> 

I try to use "choose, when" on apply template, but maybe there is a better way:

<xsl:template match="/">     <xsl:choose>         <xsl:when test="/document/line/name='MEA111'">             <xsl:apply-templates mode="MEA" select="/document"/>         </xsl:when>     </xsl:choose>     <xsl:choose>         <xsl:when test="/document/line/name='MPR111'">             <xsl:apply-templates mode="MPR" select="/document"/>         </xsl:when>     </xsl:choose>     <xsl:choose>         <xsl:when test="/document/line/name='MAR111'">             <xsl:apply-templates mode="MAR" select="/document"/>         </xsl:when>     </xsl:choose> </xsl:template> 
like image 973
Petras Avatar asked Mar 02 '11 14:03

Petras


People also ask

How do you add an if else condition in XSLT?

XSL specifications implies a function<xsl: choose> to implement the functionality of if-else statements. To work with multiple options, we are supposed to use <choose> along with <otherwise> which is equivalent to anything else. The attribute is evaluated until the processor finds the condition that is met true.

How do I write an if statement in XSLT?

The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

Can we use multiple When in XSLT?

Yes we can have multiple conditions in the same <xsl:when> element.

What is specify condition in XSLT?

The XSLT <xsl:if> element is used to specify a conditional test against the content of the XML file.


1 Answers

Actually you can merge them together:

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

EDIT: Petras, after your clarification, it seems that what you want is even easier:

<xsl:template match="/">     <xsl:if test="/document/line/name='MEA111'">        <xsl:apply-templates mode="MEA" select="/document"/>     </xsl:if>     <xsl:if test="/document/line/name='MPR111'">         <xsl:apply-templates mode="MPR" select="/document"/>     </xsl:if>     <xsl:if test="/document/line/name='MAR111'">         <xsl:apply-templates mode="MAR" select="/document"/>     </xsl:if> </xsl:template> 
like image 159
linepogl Avatar answered Sep 27 '22 18:09

linepogl