问题
I'm trying to use a regex to validate a field in my xml using xsd. I came up with the regex to do what I want which is to disallow special characters unless the text is wrapped in CDATA tags. This is the regular expression I came up with:
<!\[CDATA\[.*?\]\]>|[^<>&]*
Works great when I test it on http://regexr.com/ to match my pattern. The problem is when I try to then plug it into a simpleType pattern restriction I'm getting an error saying its not a valid regular expression.
The value '<!\[CDATA\[.*?\]\]>|[^<>&]*' of the facet 'pattern' is not a valid regular expression.
Here is my xsd code (note I had to replace &<> in the regular expression with <
>
and &
so it would be valid xml):
<xs:element name="description" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="<!\[CDATA\[.*?\]\]>|[^<>&]*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
So I assume there's something about the way regex works in xsd patterns that I'm not getting.
回答1:
By process of elimination I was able to find a fix. First I determined that the problem somewhere in the <!\[CDATA\[.*?\]\]>
part of the expression. So I tore this out of my expression and slowly adding bits back until I got the same error. The character that what causing the problem was the ?
. The expression still works without the ?
even if there is nothing inside the CDATA.
I'm not sure 1) why the person I got that pattern from included it, and 2) why doesn't xsd allow it in my pattern?
来源:https://stackoverflow.com/questions/31482388/the-value-cdata-of-the-facet-pattern-is-not-a-valid-re