Schematron& Xpath,测试有效的子元素和属性集

时间:2011-04-05 09:57:17

标签: xml validation xpath schematron

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

Fyi,有效的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" -->

欢迎任何评论或提示!

1 个答案:

答案 0 :(得分: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>