如何编写针对XML代码段的Schematron验证测试? [英] How do I write this Schematron validation test for XML snippet?

查看:204
本文介绍了如何编写针对XML代码段的Schematron验证测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML代码段:

<AAA>
    <Field name="a"/>
    <Field name="b"/>
    <Field name="x"/>
    <User id="x" id2="f"/>
    <User id="y"/>
</AAA>
<AAA>
    <Field name="r"/>
    <Field name="z"/>
</AAA>

我需要这样的规则:如果存在User标记,则应检查以查看 id id2 的Attribute值是否在Attribute名称下的领域.

I need rule such that if the User tag exists, it should check to see if the Attribute values of id and id2 exist under the name Attribute of a Field.

因此在第一个 AAA 标记中,它将验证并给出2个错误,因为 "f" 不作为字段名称存在,都不存在> "y" .

So in the first AAA tag, it will validate and give 2 errors because "f" doesn't exist as a field name and neither does "y".

AAA标签并不总是具有用户标签,并且用户标签并非总是具有 id id2 .

The AAA tags don't always have User tags, and the User tags don't always have both id and id2.

我一直在搞一些XPath表达式,但无济于事.

I've been messing around with some XPath expressions but to no avail.

推荐答案

如果不能使用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>

这篇关于如何编写针对XML代码段的Schematron验证测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆