Click here to Skip to main content
15,440,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
​ Hello,

i want to validate by xsd an attribute can contain a float type or a word "exist". after search in the net i found this solution(  but it does not worked.
how to fix that ?

What I have tried:

when i add this pattern it does not work:

<xs:restriction base="xs:Double">
      <xs:pattern value="[0-9]| 'exist'"/>
Updated 13-Jun-22 6:46am

It is a little odd that a double type can hold the value "exist". The example you've sent actually describes pattern matching on a string type.

So it is either changing the type to string or define a double value that is considered equal to "exist", e.g. double.MinValue / double.MaxValue if you're using C#.

Sharing your use case or a xml snippet could definitely help in helping with the question btw!

Share this answer
.Net Perfect Dev 13-Jun-22 12:47pm     CRLF
ok this is an exemple : xml file: <catalog> <book id="bk101" price="12.30"> <book id="bk102" price="exist"> and this is the xsd file which i tried: <xs:schema attributeformdefault="unqualified" elementformdefault="qualified" xmlns:xs=""> <xs:element name="Catalog"> <xs:complextype> <xs:sequence> <xs:element maxoccurs="unbounded" name="Book"> <xs:complextype> <xs:attribute name="id" type="xs:string" use="required"> <xs:attribute name="price" type="tprice" use="required"> <xs:simpletype name="tprice"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]| 'exist'">
ludosoep 13-Jun-22 16:15pm     CRLF
Okay, so as Dave already mentioned below, it is not possible, or at least not when using the double type. You can use xs:string, but please keep in mind it is a textual value and not a numeric value. So the only thing you seem to be missing is the . in your pattern and the single quotes that are obsolete. This simpleType should work: <xs:simpleType name="tprice"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9\.]*|exist"/> </xs:restriction> </xs:simpleType> The \. is to match the dot and the slash is to escape the dot, as this means "match all" in regex. HTH. Cheers
ok this is an exemple :
xml file:
<?xml version="1.0"?>
   <Book id="bk101" price="12.30" />
   <Book id="bk102" price="exist" />

and this is the xsd file which i tried:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="">
  <xs:element name="Catalog">
        <xs:element maxOccurs="unbounded" name="Book">
            <xs:attribute name="id" type="xs:string" use="required" />
            <xs:attribute name="price" type="tprice" use="required" />
  <xs:simpleType name="tprice">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9]| 'exist'"/>
Share this answer
Dave Kreskowiak 13-Jun-22 14:38pm     CRLF
You posted this as a solution to your own question. This should have been added to the original question details. You can't do this. Not as a double or any other numeric type. Think about it as a deserialization. How would define a field that can hold both a double and a string? You can't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900