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 \-']*"/>
</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 \-']*
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?
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.
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”>
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"/>
you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength .
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} \.\-]+$
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