469,282 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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

Nov 12 '05 #1
2 8967
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:sh********@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
Nov 12 '05 #2
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" <sh********@sitesystems.com> wrote in message
news:eY**************@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

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John L. Clark | last post: by
3 posts views Thread by ruthless | last post: by
4 posts views Thread by Mike Jansen | last post: by
1 post views Thread by Robert Mago | last post: by
2 posts views Thread by mnickel67 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.