The value '<!\[CDATA\[.*?\]\]>|[^<>&]*' of the facet 'pattern' is not a valid regular expression

和自甴很熟 提交于 2019-12-25 16:51:29

问题


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 &lt; &gt; and &amp; so it would be valid xml):

<xs:element name="description" minOccurs="1" maxOccurs="1">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:pattern value="&lt;!\[CDATA\[.*?\]\]&gt;|[^&lt;&gt;&amp;]*"/>          
        </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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!