Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between XSD:Pattern and C# Regex?

Tags:

c#

regex

xsd

I can't really go into to much depth of my project for a number of constraining reasons.

Essentially I am trying to pre-validate an object before serializing it and then validating it against a schema. The schema has validation for a name, which I know isn't ideal and your better off not validating a name - but I can't seem to replicate a valid regex for what the schema is trying to do.

<xsd:simpleType name="CharsetD">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="[A-Za-z \-&apos;]*"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:element minOccurs="0" maxOccurs="2" name="Fore">
  <xsd:simpleType>
    <xsd:restriction base="CharsetD">
      <xsd:minLength value="1"/>
      <xsd:maxLength value="35"/>
      <xsd:pattern value="[A-Za-z].*"/>
    </xsd:restriction>
  <xsd:simpleType>
</xsd:element>

I simply thought in the above case that I could try and just use the xsd:pattern for the charset.

I tried to use [A-Za-z \-&apos;]* which returned a name such as Luke2 as valid, but the schema validation said it wasn't because it contained a number.

My question is, how can I replicate the above in a single c# regex? Also, is there any differences between how the schema pattern operates compared to if I used it in .NET that I can note for the future?

like image 592
LukeHennerley Avatar asked Dec 19 '12 11:12

LukeHennerley


People also ask

Does XML support regex?

You can define regular expressions for input validation in separate XML files. Reference to these regular expressions can be used in multiple data types that are defined in the datatypes. xml file.

What is XSD enumeration?

Enumerations are a base simple type in the XSD specification containing a list of possible values. Single-valued enumerations are shown as restrictions of the base simple type xs:token , as illustrated below: ? < xs:simpleType name=”GraduationPlanTypeMapType”>

What is restriction in XSD?

restriction. restriction is normally a range of conditions to be applied on the element's value. In this example, we've set a restriction on marks that marks should be in range of 0 to 100 with both values are included. <xs:minInclusive value = "0"/> <xs:maxInclusive value = "100"/>

How is length defined in XSD?

you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength .


1 Answers

I have found the problem, albeit disguised as I haven't really worked extensively with XML Schema

The difference

The CharsetD type doesn't just use the pattern, as this alone isn't good enough to validate names with numbers, so when I tried to use just the pattern - it allowed numbers. There is a line on the type however which constrains numbers, and therefore why the schema returns an error where the regex doesn't.

<xsd:restriction base="xsd:string">

Solution

I created a different single regex which would cater the string restriction that is applied in my schema.

^[\p{L} \.\-]+$
like image 85
LukeHennerley Avatar answered Oct 19 '22 09:10

LukeHennerley