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

问题描述

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

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

例如,此castle应该验证:

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

但这不应该(在同一room中有重复的门):

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,就像我们对引文所做的那样样式语言架构.如果您采用这种方法,请注意,并非所有的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 ),然后从RELAX NG XML模式中将Schematron规则提取到独立的Schematron模式中(使用 Saxon RNG2Schtrn.xsl XSLT样式表),最后使用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>

Jing will report

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