Consider the following, XML document instance,
<?xml version="1.0"?>
<temp>ABCABD</temp>
And the following, XML Schema (XSD) 1.1 document (that'll validate the above mentioned, XML document instance),
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="temp">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(ABC)+"/>
<xs:assertion test="matches($value, '(ABC)+')"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
At first thought, as shown within the above mentioned XSD 1.1 document, it might seem that both <xs:pattern> and the <xs:assertion> would fail the validation for the XML document instance value "ABCABD" (according to the XSD document shown, the string "ABC" is shown repeating one or more times).
But in reality, and according to the XSD 1.1 specification, for the example shown above, the XML document instance value "ABCABD" would be invalid for the <xs:pattern>, but valid for <xs:assertion>. That's so because, the XPath 2.0 "matches(..)" function, returns true when any substring matches the regex, unless the "matches(..)" regex is written within ^ and & characters.
Therefore, for the above cited XSD 1.1 example, the following are exactly equivalent XSD validation checks,
<xs:pattern value="(ABC)+"/>
<xs:assertion test="matches($value, '^(ABC)+$')"/>
And for <xs:pattern>, there's no explicit regex anchoring with ^ and $ available (its implied always). i.e, with <xs:pattern>, its always the entire string input that is checked against the pattern regex.
No comments:
Post a Comment