且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编写针对XML代码段的Schematron验证测试?

更新时间:2023-11-24 16:48:04

如果不能使用XPath 2.0,则可以编写以下Schematron规则:

If you cannot use XPath 2.0, then you could write the following Schematron rules:

ISO Schematron

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">

    <sch:pattern>
        <sch:rule context="User[@id]">
            <sch:assert test="@id = ../Field/@name">User ID does not exist as a field!</sch:assert>
        </sch:rule>

        <sch:rule context="User[@id2]">
            <sch:assert test="@id2 = ../Field/@name">User ID2 does not exist as a field!</sch:assert>
        </sch:rule>
    </sch:pattern>

</sch:schema>

我假设输入的XML文档没有名称空间.如果User元素首先没有这些属性之一,或者AAA元素没有User元素,则断言不会失败.

I am assuming an input XML document that does not have a namespace. The assertion does not fail if a User element does not have one of those attributes in the first place or if an AAA element does not have a User element.

您并没有非常清楚地说明马丁·洪内(Martin Honnen)的建议为何对您不起作用,因此无论如何我都在这里列出.规则如下:

You did not say very clearly why Martin Honnen's suggestion did not work for you, so I list it here anyway. The rule would look like:

<sch:pattern>
    <sch:rule context="AAA">
        <sch:report test="some $user in User satisfies not($user/(@id, @id2) = Field/@name)">User ID does not exist as a field!</sch:report>
    </sch:rule>
</sch:pattern>