Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is IF statement allow OR condition in XSLT?

Tags:

xml

xslt

how to test if my condition is this;

<xsl:if test="node = '1' or node='2'">
<input name="list_{@id}" value="{@id}" type="checkbox"/>
</xsl:if>

Is IF statement allowed OR condition? Please advice..

like image 534
mrrsb Avatar asked Jun 08 '12 03:06

mrrsb


People also ask

How do you add an if else condition in XSLT?

If I want to test one condition (a simple if ), I use <xsl:if> . If I want to change that to test more than one condition or add an else case, I have to use <xsl:choose> , <xsl:when> , and <xsl:otherwise> .

How do I write an if statement in XSLT?

XSLT <xsl:if> 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!

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.

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

Is IF statement allowed OR condition?

No, but XPath has an or operator -- do note that XPath is case-sensitive language.

The XPath expression in the provided code:

node = '1' or node='2'

is syntactically correct.

or is a standard XPath operator and can be used to combine two subexpressions.

[33] OperatorName ::= 'and' | 'or' | 'mod' | 'div'

Here is a complete XSLT transformation example:

<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="num[ . = 3 or . = 5]"/>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

the wanted, correct result is produced (all elements copied with the exception of <num>03</num> and <num>05</num>:

<nums>
   <num>01</num>
   <num>02</num>
   <num>04</num>
   <num>06</num>
   <num>07</num>
   <num>08</num>
   <num>09</num>
   <num>10</num>
</nums>
like image 90
Dimitre Novatchev Avatar answered Oct 10 '22 03:10

Dimitre Novatchev


You can use or just like you did in your example. Here are the supported operators in XSLT.

like image 37
Oleksi Avatar answered Oct 10 '22 01:10

Oleksi