由于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>
我想测试一下;
这是我被卡住的经历;
<!-- 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" -->
欢迎任何评论或提示!
答案 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>