Why am I getting different validation between an XSD regex with Nokogiri and normal Ruby regular expressions? -
i have xsd schema includes rule specific field match following regex:
\d{8}[\-]?[a-za-z]{0,3} using irb, can test regex , following strings match, correct:
12345678 12345678- 12345678-abc 12345678abc when attempt validate xml against xsd, different behaviour:
passes: 12345678- 12345678-abc 12345678abc fails: 12345678 and here mimimal xsd/xml files reproduces this:
<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeformdefault="unqualified" elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:simpletype name="codetype"> <xs:restriction base="xs:token"> <xs:pattern value="\d{8}[\-]?[a-za-z]{0,3}"/> </xs:restriction> </xs:simpletype> <xs:element name="test"> <xs:complextype> <xs:sequence> <xs:element type="codetype" name="code"/> </xs:sequence> </xs:complextype> </xs:element> </xs:schema> and xml:
<?xml version="1.0" encoding="utf-8"?> <test> <code>11034755</code> </test> and running with
xmllint --schema test.xsd test.xml gives
element 'code': [facet 'pattern'] value '11034755' not accepted pattern '\d{8}[\-]?[a-za-z]{0,3}' while xml schema not have full regular expressions, should valid think. not understanding when comes regular expressions in xsd files in particular case regards '?' ?
using rubular test /\d{8}-?(?:[a-za-z]{3})?/ hits strings.
alternately /\d{8}[a-za-z-]*/, /\d{8}[a-z-]*/i, , /\d{8}[a-z-]{0,4}/i work also.
you can use {0,3} or {,3} might help.
Comments
Post a Comment