Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blacklist characters in xml data using xsd pattern

I guess this is a basic questions but the pattern seem quite confusing to me. I would like to blacklist some characters like + , -, @, #, <, >. If the characters are in an xml field, then I would like to invalidate the xml.

In the below example, I want to return the xml as invalid if the string field contains any of the above mentioned characters. If data is something like this "hello". It should be valid. How should I write my pattern. Thanks for checking on this.

XML:

<DataType>System.String</DataType>
<Value>
      <String>Data@yours</String>
</Value>

in XSD:

<xs:element name="Value">
<xs:complexType>
<xs:sequence>
<xs:element name="String" type="xs:string" />
</xs:sequence>
<xs:restriction base="xs:string">
<xs:pattern value="[^+-@#%&()<>?]"/>
</xs:restriction>
</xs:complexType>
</xs:element>

Further editing this part:

I have been getting these errors: An error occurred while parsing EntityName. - in my code The entity name must immediately follow the & in the entity reference in the only xsd validator tool. http://www.utilities-online.info/xsdvalidation/#.VzTwcYSDFBc

It doesn't seem to follow these special characters, How can I change this? Thanks again.

like image 923
Sailoosha Avatar asked Oct 31 '22 03:10

Sailoosha


1 Answers

Note that acc. to regular-expressions.info:

Particularly noteworthy is the complete absence of anchors like the caret and dollar, word boundaries, and lookaround. XML schema always implicitly anchors the entire regular expression. The regex must match the whole element for the element to be considered valid.

So, to match a string that does not contain +, @, #, %, &, (, ), <, >, ?, - (at least 1 symbol), you need to use

<xs:pattern value="[^+@#%&amp;()&lt;&gt;?-]+"/>

Note that <, > and & should be entitized to be used inside an XML attribute value.

If you want to allow empty value, * should be used instead of +. Note that - is put at the end of the pattern so as not to create any range like this:

enter image description here

like image 122
Wiktor Stribiżew Avatar answered Nov 09 '22 06:11

Wiktor Stribiżew