Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an exclusive OR 'XOR' in XPath?

Tags:

xslt

xpath

Is there an exclusive OR 'XOR' in XPath1.0 ?

like image 883
Christophe Debove Avatar asked Jun 27 '11 13:06

Christophe Debove


People also ask

What does character represent in XPath?

The dot, or period, character (“.”) in XPath is called the “context item expression” because it refers to the context item. This could be a node (such as an element, attribute, or text node), or an atomic value (such as a string, number, or boolean). When it's a node, it's also called the context node.

Which is not a mapping *?

Every mapping is a relation but every relation is not a mapping.

How do you use greater than and less than in XSLT?

Notice that we used &gt; instead of > in the attribute value. You're always safe using &gt; here, although some XSLT processors process the greater-than sign correctly if you use > instead. If you need to use the less-than operator ( < ), you'll have to use the &lt; entity.


2 Answers

Use this XPath 1.0 expression:

x and not(y)   or   y and not(x)

Always try to avoid the != operator, because it has an unexpected meaning/behavior when one or both of its arguments are node-sets.

In XSLT 2.0 or XQuery 1.0 one can write this as a function and then use just the function in any XPath expression. Below is an XSLT 2.0 function definition for xor and a small example of using this function:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="my:f">
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:sequence select=
  "for $x in (true(), false()),
       $y in (true(), false())
     return
       ('xor(', $x, ',', $y,') = ', f:xor($x, $y), '&#xA;')
  "/>
 </xsl:template>

 <xsl:function name="f:xor">
  <xsl:param name="pX" as="xs:boolean"/>
  <xsl:param name="pY" as="xs:boolean"/>

  <xsl:sequence select=
   "$pX and not($pY)   or   $pY and not($pX)"/>
 </xsl:function>
</xsl:stylesheet>

when this transformation is applied on any XML document (not used), the wanted, correct result is produced:

 xor( true , true ) =  false
 xor( true , false ) =  true 
 xor( false , true ) =  true 
 xor( false , false ) =  false 
like image 173
Dimitre Novatchev Avatar answered Oct 13 '22 12:10

Dimitre Novatchev


No but you can emulate it:

(a or b) and (a != b)
like image 3
Erlock Avatar answered Oct 13 '22 12:10

Erlock