本文介绍了XPath 中是否有异或“XOR"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XPath1.0 中是否存在异或XOR"?
Is there an exclusive OR 'XOR' in XPath1.0 ?
推荐答案
使用这个 XPath 1.0 表达式:
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.
在 XSLT 2.0 或 XQuery 1.0 中,可以将其编写为函数,然后在任何 XPath 表达式中只使用该函数.下面是 xor
的 XSLT 2.0 函数定义和使用此函数的小示例:
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), '
')
"/>
</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>
当此转换应用于任何 XML 文档(未使用)时,会产生所需的正确结果:
xor( true , true ) = false
xor( true , false ) = true
xor( false , true ) = true
xor( false , false ) = false
这篇关于XPath 中是否有异或“XOR"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!