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.
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="[^+@#%&()<>?-]+"/>
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With