1

由于 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' 是可选的,不允许任何其他属性
  • 只允许 'index' 或 'name' 子元素

这是我陷入困境的专家;

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

欢迎任何评论或提示!

4

1 回答 1

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>   
于 2011-04-05T12:29:37.437 回答