Click here to Skip to main content
15,881,938 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(https://www.w3schools.com/xml/schema_facets.asp)  but it does not worked.
how to fix that ?



What I have tried:

when i add this pattern it does not work:

XML
<xs:restriction base="xs:Double">
      <xs:pattern value="[0-9]| 'exist'"/>
    </xs:restriction>
Posted
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!

Cheers
 
Share this answer
 
v2
Comments
.Net Perfect Dev 13-Jun-22 12:47pm    
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="http://www.w3.org/2001/XMLSchema">
<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    
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
<?xml version="1.0"?>
<Catalog>
   <Book id="bk101" price="12.30" />
   <Book id="bk102" price="exist" />
</Catalog>



and this is the xsd file which i tried:

XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <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:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="tprice">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9]| 'exist'"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
 
Share this answer
 
Comments
Dave Kreskowiak 13-Jun-22 14:38pm    
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