【问题标题】:How do I write this Schematron validation test for XML snippet?如何为 XML 片段编写此 Schematron 验证测试?
【发布时间】:2017-01-27 20:34:51
【问题描述】:

我有一个这样的 XML sn-p:

<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>

我需要这样的规则,如果用户标签存在,它应该检查 idid2 的属性值是否存在于名称属性下的一个字段。

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

AAA 标记并不总是有用户标记,用户标记也不总是同时具有 idid2

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

【问题讨论】:

  • 您将 Schematron 与 XPath 2.0 还是 1.0 一起使用?在 XPath 2.0 中,您可以轻松编写 some $user in User satisfies not($user/(@id, @id2) = Field/@name
  • 那还不够。
  • 请显示完整的输入 XML 文档。谢谢!此外,“那并没有完全做到”不是一个非常精确的错误消息。究竟是什么不起作用?

标签: xml xslt xpath xml-validation schematron


【解决方案1】:

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

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 元素,则断言不会失败。

你没有说得很清楚,为什么 Martin Honnen 的建议对你不起作用,所以我还是把它列在这里。规则如下所示:

<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>

【讨论】:

  • 非常感谢!这做到了。我无法显示堆栈跟踪或更具体的内容,因为我无法调试我的 schematron 断言。我不知道我得到了什么,但我知道我得到的结果是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
  • 2011-02-21
相关资源
最近更新 更多