Home All Groups Group Topic Archive Search About

Restrictions on Attributes in XML Schema.

Author
15 Nov 2004 10:04 PM
Shailendra Batham
Hello Gurus,

I want to put some restrictions on my attribute tag in my XML Schema, anyone out there have any idea how to do that.

here is my XML and the XML Schema

<?xml version="1.0" encoding="utf-8"?>
    <Book ID="1000000000">
        <Name>XML Basic</Name>
        <Comments>Whatever</Comments>
    </Book>
</Survey>


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Comments" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="ID" type="xs:integer" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

I want to put some restrictions on the ID, eg, the ID cannot be more than 10000........... so how do i add restrictions to attributes, I already know restrictions for elements but that is not helping me for attributes.

help is appreciated,
Thanks
shailendra batham

Author
16 Nov 2004 4:33 AM
Christoph Schittko [MVP]
You have to define a simpleType and define the restrictions on the
values for the attribute in the type definition as in the schema below.



Check out the <simpleType> definition that defines a new type based on
xs:integer but with the <restriction> that the maximum value of a valid
instance of the type is 99.



The attribute definition for the ID attribute uses that type to enforce
the restrictions on your ID



<?xml version="1.0" encoding="utf-8" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:myname" xmlns:tns="urn:myname"
elementFormDefault="qualified">  

    <xs:simpleType name="myidtype">

    <xs:restriction base="xs:integer">

        <xs:maxExclusive value="100" />

    </xs:restriction>

    </xs:simpleType>

    <xs:element name="Book">

        <xs:complexType>

            <xs:sequence>

                <xs:element name="Name" type="xs:string"/>

                <xs:element name="Comments" type="xs:string"/>

            </xs:sequence>

            <xs:attribute name="ID" type="tns:myidtype" use="required"/>

        </xs:complexType>

    </xs:element>

</xs:schema>



There are a number of other possibilities for the <restrictions> which
you can look up in a good discussion of XML Schema or in the XML schema
spec at [0]



HTH,

Christoph Schittko

MS MVP XML

http://weblogs.asp.net/cschittko



[0] http://www.w3.org/XML/Schema.html#dev



________________________________

From: Shailendra Batham [mailto:shailen***@sitesystems.com]
Posted At: Monday, November 15, 2004 4:05 PM
Posted To: microsoft.public.dotnet.xml
Conversation: Restrictions on Attributes in XML Schema.
Subject: Restrictions on Attributes in XML Schema.


Hello Gurus,



I want to put some restrictions on my attribute tag in my XML Schema,
anyone out there have any idea how to do that.



here is my XML and the XML Schema


<?xml version="1.0" encoding="utf-8"?>
    <Book ID="1000000000">
        <Name>XML Basic</Name>
        <Comments>Whatever</Comments>

    </Book>
</Survey>



<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Comments" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="ID" type="xs:integer" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

I want to put some restrictions on the ID, eg, the ID cannot be more
than 10000........... so how do i add restrictions to attributes, I
already know restrictions for elements but that is not helping me for
attributes.

help is appreciated,
Thanks
shailendra batham
Author
16 Nov 2004 10:45 AM
Han
Try,

<xs:simpleType name="type1">
<xs:restriction base="xs:integer">
  <xs:maxInclusive value='10'/>
</xs:restriction>
</xs:simpleType>

    <xs:element name="Book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Comments" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="ID" type="type1" use="required"/>
        </xs:complexType>
    </xs:element>

--
Pohwan Han. Seoul. Have a nice day.
"Shailendra Batham" <shailen***@sitesystems.com> wrote in message
news:eYp8g81yEHA.3708@TK2MSFTNGP14.phx.gbl...
Hello Gurus,

I want to put some restrictions on my attribute tag in my XML Schema, anyone
out there have any idea how to do that.

here is my XML and the XML Schema

<?xml version="1.0" encoding="utf-8"?>
    <Book ID="1000000000">
        <Name>XML Basic</Name>
        <Comments>Whatever</Comments>
    </Book>
</Survey>


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string"/>
                <xs:element name="Comments" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="ID" type="xs:integer" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

I want to put some restrictions on the ID, eg, the ID cannot be more than
10000........... so how do i add restrictions to attributes, I already know
restrictions for elements but that is not helping me for attributes.

help is appreciated,
Thanks
shailendra batham

AddThis Social Bookmark Button