I want to restrict a string type attribute with a pattern. The attribute looks like this:
我想用模式限制字符串类型属性。该属性如下所示:
text(x,y,z)
for 0 <= x,y,z <= 255
So the 3 numbers inside the parenthesis must be restrictet to maximum values of 255.
因此,括号内的3个数字必须限制为最大值255。
Valid example:
text(24,0,255)
Invalid example:
text(15,635,5)
I tried already this, but it doesn't work:
我已经尝试了这个,但它不起作用:
<xs:restriction base="xs:string">
<xs:pattern value="text\([0-255],[0-255],[0-255]\)"/>
</xs:restriction>
Then i thought of doing something like this:
然后我想做这样的事情:
<xs:restriction base="xs:string">
<xs:pattern value="text\(1?[0-9][0-9],1?[0-9][0-9],1?[0-9][0-9]\)|text\(2[0-4][0-9],1?[0-9][0-9],1?[0-9][0-9]\)|text\(25[0-5],1?[0-9][0-9],1?[0-9][0-9]\)|..."/>
</xs:restriction>
But as there are 3 possiblities for each of the 3 numbers, there would be 27 combinations.
但由于3个数字中的每一个都有3种可能性,因此将有27种组合。
Is there any other possibility to restrict numbers to maximum and minimum values in a string type than restricting each digit and combine all possibilities with an "or"?
是否有其他可能性将数字限制为字符串类型中的最大值和最小值,而不是限制每个数字并将所有可能性与“或”组合?
1 个解决方案
#1
0
In XSD 1.0 regular expressions are the only solution available. It gets pretty nasty, as you've found, but it can be done. One neat trick that I have seen is to build reusable regex fragments using entity references:
在XSD 1.0中,正则表达式是唯一可用的解决方案。正如你所发现的那样,它变得非常讨厌,但它可以做到。我见过的一个巧妙的技巧是使用实体引用构建可重用的正则表达式片段:
<!ENTITY n255 "[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]">
<xs:pattern value="text\(&n255;,&n255;,&n255;\)"/>
I think I also came across a tool that automates the production of a regex to match integers in a given range, but I don't recall where.
我想我也遇到了一个工具,可以自动生成正则表达式以匹配给定范围内的整数,但我不记得在哪里。
In XSD 1.1 it might be better to combine a regex with an assertion:
在XSD 1.1中,将正则表达式与断言组合起来可能更好:
<xs:pattern value="text\([0-9]+,[0-9]+,[0-9]+)"/>
<xs:assertion test="every $t in tokenize(substring-before(substring-after($value, '('), ')'), ',') satisfies (number($t) ge 0 and number($t) le 255)"/>
#1
0
In XSD 1.0 regular expressions are the only solution available. It gets pretty nasty, as you've found, but it can be done. One neat trick that I have seen is to build reusable regex fragments using entity references:
在XSD 1.0中,正则表达式是唯一可用的解决方案。正如你所发现的那样,它变得非常讨厌,但它可以做到。我见过的一个巧妙的技巧是使用实体引用构建可重用的正则表达式片段:
<!ENTITY n255 "[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]">
<xs:pattern value="text\(&n255;,&n255;,&n255;\)"/>
I think I also came across a tool that automates the production of a regex to match integers in a given range, but I don't recall where.
我想我也遇到了一个工具,可以自动生成正则表达式以匹配给定范围内的整数,但我不记得在哪里。
In XSD 1.1 it might be better to combine a regex with an assertion:
在XSD 1.1中,将正则表达式与断言组合起来可能更好:
<xs:pattern value="text\([0-9]+,[0-9]+,[0-9]+)"/>
<xs:assertion test="every $t in tokenize(substring-before(substring-after($value, '('), ')'), ',') satisfies (number($t) ge 0 and number($t) le 255)"/>