3

我想使用 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'

在其中自我工作。它正确地返回真或假。但是对它使用 count 是行不通的。(似乎总是返回真)

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

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

任何帮助将不胜感激。

谢谢你,e


编辑:

在 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' 和 elem2/text() = 'test2']) > 0)

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

4

2 回答 2

3

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

使用谓词测试text()值并计算有多少满足条件:

count(./group/elem1/text()[.='test1'])
于 2016-05-10T15:08:29.053 回答
0

答案:

<?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' 
                                and elem2/text() = 'test2']) > 0) 
                                and (count(group[elem1/text() = 'something1' 
                                and elem2/text() = 'something2']) > 0 )"/>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>

将验证:

<root>
    <group>
        <elem1>test1</elem1>
        <elem2>test2</elem2>
    </group>
    <group>
        <elem1>test1</elem1>
        <elem2>test4</elem2>
    </group>
    <group>
        <elem1>something1</elem1>
        <elem2>something2</elem2>
    </group>
    <group>
        <elem1>other1</elem1>
        <elem2>other2</elem2>
    </group>
</root>
于 2016-05-19T13:04:25.297 回答