【问题标题】:Schematron & Xpath, testing for valid set of sub-elements and attributesSchematron 和 Xpath,测试有效的子元素和属性集
【发布时间】:2011-07-29 20:04:47
【问题描述】:

由于 XSD 不能满足我的需求,我现在正在评估 Schematron。对允许的元素以外的元素进行测试似乎是不可能的。 我需要验证@type="colorSet".. 接下来的测试对我来说最重要:

any other than this set of attributes

any other than this set of optional elements

其次如何check if values are alphanumerical or digits

仅供参考,有效的 XML 选项是;

<defaultBgColor type="colorSet" r="255" g="255" b="255"/>
<defaultBgColor type="colorSet" r="255" g="255" b="255" a="100"/>
<paint type="colorSet">
  <index>140</index>
</paint>
<paint type="colorSet">
  <name>blue</name>
</paint>

我想测试;

  • 'R','G','B' 必须存在
  • 'A' 是可选的,不允许任何其他属性
  • 仅允许使用“索引”或“名称”子元素

这是我陷入困境的专家;

    <!-- Don't know how to do the second assert? -->
    <rule context="//*[@type='colorSet' and count(child::*) = 0]">
      <assert test="@r and @g and @b"           >One of R G B missing</assert>
      <assert test="any other than @r,@g,@b,@a" >Invalid attribute   </assert>
    </rule>

    <!-- is a test like isNumber possible? -->
    <assert test="isNumber( text() )">Index is not a number</assert>

    <!-- is a test like isAlpha possible? -->
    <assert test="isAlpha( substring(text(),1) )">Invalid name</assert>

    <!-- How to assert "any other than valid (optional) elements" -->

欢迎任何评论或提示!

【问题讨论】:

  • 我会说 XML Schema 可以验证这些约束。 XML 模式不能满足您的需求还有其他原因吗?
  • 嗯.. 我认为我需要的主要内容以及 XML Schema 不支持的内容。像 XYZ 可以是任何元素。

标签: xml validation xpath schematron


【解决方案1】:

我认为你需要:

<assert test="count(@*) > count(@r|@g|@b|@a)" >Invalid attribute   </assert> 

<assert test="number(.) = number(.)">Index is not a number</assert> 

<!-- It depends on what you mean: "does it start with no digit" -->
<assert test="not(contains('0123456789',substring(.,1,1)))"
       >Invalid name</assert>   

【讨论】:

  • 有点创意可以做的事情令人惊叹:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
相关资源
最近更新 更多