Domanda

I am having a hard time doing a restriction for an XML tag.

XML tag name = price, value = $150

Restrictions: price must contain a '$' followed by a float number between 0 and 400.

I need an XSD definition for price with the above restriction.

È stato utile?

Soluzione

This XSD:

<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="prices">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="price" maxOccurs="unbounded">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:pattern value="\$[1-3]?[0-9]?[0-9]?(\.[0-9][0-9])?|(\$400(\.00)?)"/>
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Allows these prices:

<?xml version="1.0" encoding="utf-8" ?>
<prices>
  <price>$0</price>
  <price>$1</price>
  <price>$1.00</price>
  <price>$1.99</price>
  <price>$400.00</price>
  <price>$400</price>
  <price>$.99</price>
</prices>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top