开发者

Regular expression in XML schema definition fails

开发者 https://www.devze.com 2023-01-29 06:42 出处:网络
I get the upcoming error while validating the XML against the schema. Value \'this/is/a/simple/node-path\' is not facet-valid

I get the upcoming error while validating the XML against the schema.

Value 'this/is/a/simple/node-path' is not facet-valid 
with respect to pattern '^(\w+[\w\-/])+\w' for type 'PathModel'.

The definition of type PathModel is defined as a simpleType as the following snippet shows. It i开发者_如何学JAVAs used by <path>this/is/a/simple/node-path</path>

<xs:simpleType name="PathModel">
  <xs:restriction base="xs:string">
    <xs:pattern value="^(\w+[\w\-/])+\w" />
  </xs:restriction>
</xs:simpleType>

The expected result is listed in this matching table.

this/is/a/simple/node-path       MATCHING
/this/is/a/simple/node-path      NOT MATCHING
this/is/a/simple/node-path/      NOT MATCHING
this/is/a/simple/nodep%th        NOT MATCHING (special characters)

What is going wrong? Thank you


Remove the leading ^ character.


<xs:simpleType name="PathModel">
  <xs:restriction base="xs:string">
    <xs:pattern value="(\w+[\w\-/])+\w" />
  </xs:restriction>
</xs:simpleType>

This is the only valid value from the set you provided:


this/is/a/simple/node-path

This should do the trick for you (tested in my Eclipse IDE).

The reason is visible, for example, here: http://www.regular-expressions.info/xml.html

"Compared with other regular expression flavors, the XML schema flavor is quite limited in features. Since it's only used to validate whether an entire element matches a pattern or not, rather than for extracting matches from large blocks of data. XML schema always implicitly anchors the entire regular expression. The regex must match the whole element for the element to be considered valid. If you have the pattern regexp, the XML schema validator will apply it in the same way as say Perl, Java or .NET would do with the pattern ^regexp$."


As far as I can see the regular expression seems to be valid and should give the results shown in your matching table, according to the XSD specification. Using Microsoft .NET Framework 2 with a test case using your path values and your regular expression I get exactly your expected results.

So, possible reasons for what you are seeing:

  • There is a bug in the XSD implementation you are using.
  • Your code isn't validating what you think it is validating (though this seems unlikely given the error message you are seeing)

If you tell us what implementation you are using and post your code, it may be possible to help further.

0

精彩评论

暂无评论...
验证码 换一张
取 消