Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL if else condition

Tags:

xml

xslt

xpath

I have a requirement where I'd like to have if else statement to check whether a node has attributes or it has just string.

Eg: 1 of the node has 0 File(s) found and the other has attribs such as <autoincludesystem_info mdate='08/23/2011' mtime='09:51' ampm='PM' filesize='64' filename='AFP_p.tgp' />

Below is a sample of two nodes

<product> <autoIncludeUser>0 File(s) found</autoIncludeUser> <autoIncludeSystem> <autoincludesystem_info mdate='08/23/2011' mtime='09:51' ampm='PM' filesize='64' filename='AFP_p.tgp' /> <autoincludesystem_info mdate='08/23/2011' mtime='09:51' ampm='PM' filesize='3,879' filename='AnalystsExpressionMacros.tgp' /> <autoincludesystem_info mdate='08/23/2011' mtime='09:51' ampm='PM' filesize='475' filename='base64Converter.tgp' /> <autoincludesystem_info mdate='08/23/2011' mtime='09:51' ampm='PM' filesize='&lt;DIR&gt;' filename='codePages' /> </autoIncludeSystem> <autoIncludeStudio>0 File(s) found</autoIncludeStudio> <externalLibrarySystem> <externalLibrarySystem_info mdate='08/23/2011' mtime='09:52' ampm='PM' filesize='196,608' filename='AFPtoXML_DP.dll' /> <externalLibrarySystem_info mdate='08/23/2011' mtime='09:52' ampm='PM' filesize='13,259' filename='ASN1toXSDRunner.jar' /> <externalLibrarySystem> </product> 

How would i identify if a node has just strings or attribs and based on that I can get the values either String or attrib values respectively.

like image 790
AabinGunz Avatar asked Aug 30 '11 06:08

AabinGunz


People also ask

How do I use if else condition in XSLT?

The <xsl:if> element contains a template that will be applied only if a specified condition is true. Tip: Use <xsl:choose> in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests!

Can we use if condition in XML?

You can't, as such: XML isn't a programming language. But you can have conditional criteria in a Schema, DTD, or a processor, and some DTDs provide attributes for conditional processing. If you need to make an element optional, based on some internal or external criteria, you can do so in a Schema.

How do you use greater than in XSLT?

Also, in XSLT 2.0, you can use the operators "gt" (greater than), "lt" (less than), and "eq" (equal).


1 Answers

We can achieve if else by using below code

<xsl:choose>     <xsl:when test="something to test">      </xsl:when>     <xsl:otherwise>      </xsl:otherwise> </xsl:choose> 

So here is what I did

<h3>System</h3>     <xsl:choose>         <xsl:when test="autoIncludeSystem/autoincludesystem_info/@mdate"> <!-- if attribute exists-->             <p>                 <dd><table border="1">                     <tbody>                         <tr>                             <th>File Name</th>                             <th>File Size</th>                             <th>Date</th>                             <th>Time</th>                             <th>AM/PM</th>                         </tr>                         <xsl:for-each select="autoIncludeSystem/autoincludesystem_info">                             <tr>                                 <td valign="top" ><xsl:value-of select="@filename"/></td>                                 <td valign="top" ><xsl:value-of select="@filesize"/></td>                                 <td valign="top" ><xsl:value-of select="@mdate"/></td>                                 <td valign="top" ><xsl:value-of select="@mtime"/></td>                                 <td valign="top" ><xsl:value-of select="@ampm"/></td>                             </tr>                         </xsl:for-each>                     </tbody>                 </table>                 </dd>             </p>         </xsl:when>         <xsl:otherwise> <!-- if attribute does not exists -->             <dd><pre>                 <xsl:value-of select="autoIncludeSystem"/><br/>             </pre></dd> <br/>         </xsl:otherwise>     </xsl:choose> 

My Output

enter image description here

like image 185
AabinGunz Avatar answered Nov 04 '22 09:11

AabinGunz