【发布时间】:2016-08-08 07:42:34
【问题描述】:
输入是这样的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<input>
<Scopes>
<Scope>Issuance</Scope>
<Scope>TimeSeries</Scope>
<Scope>MonthlyDisclosures</Scope>
</Scopes>
<AsyncFlag>true</AsyncFlag>
</input>
两个不同的开发人员编写了 XPATH 来评估这个 XML 文档;他们的 XPATH 的要点是:
“如果<Scope>MonthlyCollateral</Scope>或<Scope>MegaUltimateCollateral</Scope>在输入文档中,并且<AsyncFlag> = true,则执行以下XSLT块”。
开发人员以这种形式编写了她的 XPATH:
<xsl:if test="(exists(/input/Scopes[Scope='MonthlyCollateral']) or exists(/input/Scopes[Scope='MegaUltimateCollateral' ])) and /input/AsyncFlag='true' ">
开发者二以这种形式编写了他的 XPATH:
<xsl:if test="exists(/input/Scopes[Scope='MonthlyCollateral'] or /input/Scopes[Scope='MegaUltimateCollateral' ]) and /input/AsyncFlag='true' ">
版本一使用两个单独的 exists() 调用和一个“或”组合,而版本二使用一个包含对 XPATH 进行或运算的 exists() 调用。
在我的帖子顶部显示的 XML 文档中,这两个测试都应该评估为假;但是,只有第一个返回预期结果为 false。
在阅读了 Michael 的回复后,我决定将其提炼为本质——“为什么存在(xpath-1 或 xpath-2)在 xpath-1 和 xpath-2 均未评估为 true 时评估为 true。
当针对 XML 文档进行评估时,以下内容返回 true:
exists(/input/Scopes[Scope='Issuance'])
当针对 XML 文档进行评估时,以下内容返回 false:
exists(/input/Scopes[Scope='MegaUltimateCollateral'])
当针对 XML 文档进行评估时,以下内容返回 true:
exists(/input/Scopes[Scope='MegaUltimateCollateral'] or /input/Scopes[Scope='Issuance'])
当针对 XML 文档进行评估时,以下内容返回 true:
exists(/input/Scopes[Scope='MegaUltimateCollateral'] or /input/Scopes[Scope='MonthlyCollateral'])
前三个对我来说很有意义——至少有一个 XPATH 确实选择了 a ,因此它确实“存在”。然而,最后一个对我来说仍然没有意义。 /input/Scopes 中既不存在 MegaUltimateCollateral 也不存在 MonthlyCollateral,因此在我(可能是不合时宜的)阅读“或”时,exists() 应该评估为 false,而不是 true。
【问题讨论】:
-
"看了 Michael 的回复,我决定提炼它的本质.." 本质还是和以前一样:
exists()函数只有一个争论。当该参数是一个表达式时 - 就像在您的示例中一样 - 然后首先评估该参数,然后exists()函数对该评估的结果进行操作。在这种情况下,评估结果将是true或false。exists()函数仅在其参数为空序列时返回false。true和false都不是空序列。 -
P.S.也许您将
exists(a or b)与exists(a | b)混淆了。前者将始终为true,而当a和b都不存在时(即a和b的并集为空时,后者将返回false序列)。 -
您的问题没有得到解答吗?
-
是的,Michael.hor257K 为我回答了我的问题,我投了赞成票。
-
如果您的问题得到解答,请通过accepting the answer关闭它。 -- “我投了赞成票。”不,你没有。