noelfabro
noelfabro

Reputation: 21

how to define a range in xsd and xsd restrictions

i am currently studying xml, xsd and xsl i have this question that i need to define an id as a positive integer in the range of 1000 to 2000. the attribute is required and there must be only one id per person.

here's the xml

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<?xml-stylesheet type="text/xsl" href="items.xsl" ?>

<sales>
    <sale>
        <id>1027</id>
        <firstName>Elaine</firstName>
        <lastName>Conner</lastName>
        <street>Ap #177-657 Pede Avenue</street>
        <city>Miller</city>
        <postCode>2143</postCode>
        <salesQuantity>100</salesQuantity>
        <productId>p101</productId>
    </sale>
    <sale>
        <id>1105</id>
        <firstName>Shelby</firstName>
        <lastName>Hinton</lastName>
        <street>P.O. Box 551, 5296 Penatibus Rd</street>
        <city>Granville</city>
        <postCode>2142</postCode>
        <salesQuantity>75</salesQuantity>
        <productId>p201</productId>
    </sale>
    <sale>
        <id>1122</id>
        <firstName>Bryar</firstName>
        <lastName>Weiss</lastName>
        <street>P.O. Box 511, 5053 Ac Avenue</street>
        <city>Campsie</city>
        <postCode>2153</postCode>
        <salesQuantity>150</salesQuantity>
        <productId>p105</productId>
    </sale>
    <sale>
        <id>1037</id>
        <firstName>Slade</firstName>
        <lastName>Haynes</lastName>
        <street>593-252 Nullam Rd</street>
        <city>Lidcombe</city>
        <postCode>2145</postCode>
        <salesQuantity>100</salesQuantity>
        <productId>p101</productId>
    </sale>
</sales>
  1. can you please let me know if i got this right with the id and how can i create the range from 1000 to 2000 in the id?? i was thinking its the minOccurs and maxOccurs but that can't be right?? maxOccurence is the possible maximum "occurence" attributes and not the range, right? and i used the mimOccurence="1" because there must be only 1 id per person??

  2. with my productId, i have to set restrictions with 4 alpha numeric characters. the value in the pattern must be an uppercase character, followed by 3 numbers. can you please confirm if i did it correctly?

here's the xsd that i'm working on.

<?xml version = "1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sales">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sale" type="mySale" minOccurs="1" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="mySale">
        <xs:sequence>
            <xs:element name="id" type="xs:positiveInteger" minOccurs="1" use="required" />
            <xs:element name="firstName" type="xs:string" use="required" />
            <xs:element name="lastName" type="xs:string" use="required" />
            <xs:element name="street" type="xs:string" />
            <xs:element name="city" type="xs:string" />
            <xs:element name="postCode" type="xs:positiveInteger" use="required" />
            <xs:element name="salesQuantity" type="xs:integer" />
            <xs:element name="productId">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:pattern value="[A-Z0-90-90-9]{4}" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

thank you in advance

Upvotes: 1

Views: 8998

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

Perhaps you need to do a little more reading?

For the id attribute, define its type as a user-defined type that is derived by restriction from xs:integer with two facets: minInclusive and maxInclusive

<xs:simpleType name="IdType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="1000"/>
    <xs:maxInclusive value="2000"/>
  </xs:restriction>
</xs:simpleType>

For the productId, the pattern you want is [A-Z][0-9][0-9][0-9].

Upvotes: -1

kjhughes
kjhughes

Reputation: 111551

Define an id as a positive integer in the range of 1000 to 2000.

Use xsd:minInclusive or xsd:maxInclusive:

  <xsd:simpleType name="Id1000to2000Type">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="1000"/>
      <xsd:maxInclusive value="2000"/>
    </xsd:restriction>
  </xsd:simpleType>

The attribute is required and there must be only one id per person.

Careful. In XML, attribute means something very specific; id is an element here.

<xsd:element name="id" minOccurs="1" maxOccurs="1" type="Id1000to2000Type"/>

Note that since the default values for minOccurs and maxOccurs are 1, you may omit them in the above declaration.

with my productId, I have to set restrictions with 4 alphanumeric characters. The value in the pattern must be an uppercase character, followed by 3 numbers. Can you please confirm if I did it correctly?

    <xs:pattern value="[A-Z0-90-90-9]{4}" />

No, your pattern would allow uppercase letters to appear mixed among the digits, and would not require that one be present. You also shouldn't repeat 0-9 ranges in additional to the {4}.

Instead, use one of the following equivalent expressions of your constraint:

     <xs:pattern value="[A-Z][0-9][0-9][0-9]" />
     <xs:pattern value="[A-Z][0-9]{3}" />
     <xs:pattern value="[A-Z]\d{3}" />

Upvotes: 3

Related Questions