如何使用 RelaxNG 检查属性是否唯一? [英] How to check that attributes are unique with RelaxNG?

查看:13
本文介绍了如何使用 RelaxNG 检查属性是否唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 RelaxNG,我可以检查属性的值在封闭元素中是否唯一吗?

With RelaxNG, can I check whether or not the value of an attribute is unique within an enclosing element?

例如,这个城堡应该验证:

<castle>
  <room>
    <door to="North" />
    <door to="South" />
  </room>
  <room>
    <door to="North" />
  </room>
</castle>

但这不应该(在同一个房间中重复门):

But this should not (duplicate door in same room):

<castle>
  <room>
    <door to="Dungeon" />
    <door to="Dungeon" />
  </room>
</castle>

我正在使用 RelaxNG(紧凑型).我不知道提前"的属性值,只知道它们在 room 中应该是唯一的.

I'm using RelaxNG (compact). I don't know the attribute values 'ahead of time', only that they should be unique within a room.

谢谢!

推荐答案

据我所知,这不能在纯 RELAX NG 中完成.您可以使用(嵌入式)Schematron,就像我们对 Citation 所做的那样样式语言架构.如果您确实采用此方法,请注意并非所有 RELAX NG 验证器都解析嵌入式 Schematron,并且对独立 Schematron 模式的支持也受到限制.例如.流行的 Jing XML 验证器仅支持较旧的 Schematron 1.5 版本,不支持较新的 ISO Schematron.

To my knowledge this can't be done in pure RELAX NG. You could use (embedded) Schematron, as we did for the Citation Style Language schema. If you do take this route, note that not all RELAX NG validators parse embedded Schematron, and that support for standalone Schematron schemas is also limited. E.g. the popular Jing XML validator only supports the older Schematron 1.5 version, not the newer ISO Schematron.

对于我们使用 Jing 的项目,我们使用 脚本 首先将我们的 RELAX NG Compact 模式转换为 RELAX NG XML 格式(使用 Trang),然后将 Schematron 规则从 RELAX NG XML 模式中提取到独立的 Schematron 模式中(使用 撒克逊人RNG2Schtrn.xslXSLT 样式表),最后使用 Jing 对提取的 Schematron 模式进行验证.

For our project, where we use Jing, we use a script to first convert our RELAX NG Compact schema to the RELAX NG XML format (with Trang), then extract the Schematron rules from the RELAX NG XML schema into a standalone Schematron schema (with Saxon and the RNG2Schtrn.xsl XSLT style sheet), and finally validate against the extracted Schematron schema with Jing.

如果这还没有吓到你,我为你的问题拼凑了以下 Schematron 1.5 架构:

If this hasn't scared you off, I cobbled together the following Schematron 1.5 schema for your problem:

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://www.ascc.net/xml/schematron">
  <sch:pattern name="duplicateAttributeValues">
    <sch:rule context="//room/door[@to]">
      <sch:report test="preceding-sibling::door/@to = @to">Warning: @to values should be unique for a given room.</sch:report>
    </sch:rule>
  </sch:pattern>
</sch:schema>

在以下 XML 文档上运行时,

When run on the following XML document,

<?xml version="1.0" encoding="utf-8"?>
<castle>
  <room>
    <door to="North"/>
    <door to="South"/>
    <door to="West"/>
  </room>
  <room>
    <door to="West"/>
    <door to="North"/>
    <door to="West"/>
  </room>
</castle>

荆将报告

错误:警告:@to 值对于给定房间应该是唯一的.
从第 11 行第 5 列开始;到第 11 行第 21 列
th"/>↩ <door to="West"/>↩ </r

Error: Warning: @to values should be unique for a given room.
From line 11, column 5; to line 11, column 21
th"/>↩ <door to="West"/>↩ </r

这篇关于如何使用 RelaxNG 检查属性是否唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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