Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean operator OR in where clause using XSLT

Tags:

xslt

Hello folks, I am trying to set a fixed value to a tag in XML on comparing to a value in condition. such as

<xsl:when test="(//TestInput='XYZA') OR (//TestInput='XYZB') OR (//TestInput='XYZC') OR (//TestInput='XYZD')">abcd</xsl:when>

when i trying to run the transformation with an XML with tag <TestInput>, it is giving me an error as

Extra illegal tokens: '(', '/', '/', 'TestInput', '=', ''XYZA'', ')', 'OR', '(', '/', '/', 'TestInput', '=', ''XYZB'', ')', 'OR', '(', '/', '/', 'TestInput', '=', ''XYZC'', ')', 'OR', '(', '/', '/', 'TestInput', '=', ''XYZD'', ')'

Please help me out in setting the value to this tag based on condition using OR operator in where clause.

Thanks in Advance

like image 545
user1541472 Avatar asked Jul 23 '12 17:07

user1541472


2 Answers

Case matters with XML/XSLT/XPath so use or instead of OR.

like image 127
Martin Honnen Avatar answered Sep 28 '22 16:09

Martin Honnen


I created a test with the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Test>
  <Item>DAC</Item>
  <Item>DAD</Item>
  <Item>DAE</Item>
</Test>

And the following XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
      <test>
      <xsl:choose>
        <xsl:when test="//Item[text()='DAC'] or //Item[text()='DAE']">
          <output>Here is the text!</output>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
      </xsl:choose>
      </test>
    </xsl:template>
</xsl:stylesheet>

I get the following output:

<?xml version="1.0" encoding="utf-8"?>
<test>
  <output>Here is the text!</output>
</test>

Tested with Micorosft.Net's XmlDocument class. The critical difference is the case of the 'or' statement.

like image 24
dash Avatar answered Sep 28 '22 15:09

dash