0"always true,,即使字符串不匹配-6ren"> 0"always true,,即使字符串不匹配-我想使用 XSD1.1 断言功能来验证内容级别的元素。(更准确地说,我想检查以 XML 表示的 EDIFACT 中是否存在内容组合,但这不是重点...) 为了测试我的 XPath,我构建了以下小型测试-6ren">
gpt4 book ai didi

xml - XSD 中的 X-Path 2.0(断言): "count(//elem/text() = ' test') > 0"always true,,即使字符串不匹配

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:06 26 4
gpt4 key购买 nike

我想使用 XSD1.1 断言功能来验证内容级别的元素。(更准确地说,我想检查以 XML 表示的 EDIFACT 中是否存在内容组合,但这不是重点...)

为了测试我的 XPath,我构建了以下小型测试场景:

XML

<root>
<group>
<elem1>test1</elem1>
<elem2>test2</elem2>
</group>
<group>
<elem1>something1</elem1>
<elem2>something2</elem2>
</group>
<group>
<elem1>other1</elem1>
<elem2>other2</elem2>
</group>
</root>

要求是:我想检查,我有 test1 + test2 字符串的组合,以及 something1 和 something2 字符串的组合。可能有像 other1 + other2 组这样的组,可以有,但我不在乎。这里三组的顺序应该也没有影响。

我要测试的 XSD 是:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>

<xsd:element name="group" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="elem1" minOccurs="1">
</xsd:element>
<xsd:element name="elem2" minOccurs="1">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:sequence>
<xsd:assert test="(count(./group/elem1/text() = 'test1') > 0
and count(./group/elem2/text() = 'test2') > 0)
and (count(./group/elem1/text() = 'something1') > 0
and count(./group/elem2/text() = 'something2') > 0)"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>

有趣的是:

(count(./group/elem1/text() = 'test1') > 0 
and count(./group/elem2/text() = 'test2') > 0)
and (count(./group/elem1/text() = 'something1') > 0
and count(./group/elem2/text() = 'something2') > 0)

或将其分解:

count(./group/elem1/text() = 'test1') > 0

我的问题是:即使字符串不匹配,表达式(更具体地说是计数)返回 true。比方说,我针对“test1”进行测试,但我的字符串是“test”:

./group/elem1/text() = 'test1'

在其中 self 运作。它正确地返回 true 或 false。但是对它使用计数是行不通的。 (似乎总是返回 true)

我假设,在这里计数不是正确的解决方案,问题是,我不想测试每个组“它是完全正确的”,但毕竟所有组“这个和这个特定的组合至少出现一次”组的所有重复。

我正在 Saxon 9 EE 上对此进行测试,但 XPath 在其他 XPath 实现上也具有相同的行为。

如有任何帮助,我们将不胜感激。

谢谢,电子


编辑:

在 Mads Hansen 和 Michael Kay(谢谢!)的帮助下实现这一目标后,我还有最后一个障碍要跨越:

考虑这种情况:

<root>
<group>
<elem1>test1</elem1>
<elem2>WRONG</elem2>
</group>
<group>
<elem1>WRONG</elem1>
<elem2>test2</elem2>
</group>
</root>

用这个 XPath

计数(组[elem1/text() = 'test1' and elem2/text() = 'test2']) > 0)

这现在导致上面的示例无效(正如我所希望的那样),而我已经验证了上面的原始 XPath,因为它没有在 .

最佳答案

您需要调整 XPath 以过滤您要查找的项目,然后统计剩余的项目。您当前的表达式正在评估 group/elem1/text() 节点中的任何一个是否等于 test1,这将是 true()false(),然后计算 bool 值。

使用谓词测试 text() 值并计算满足条件的数量:

count(./group/elem1/text()[.='test1'])

关于xml - XSD 中的 X-Path 2.0(断言): "count(//elem/text() = ' test') > 0"always true,,即使字符串不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com