473,769 Members | 7,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Whate ver</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:intege r" 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 9235
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"
elementFormDefa ult="qualified" >

<xs:simpleTyp e name="myidtype" >

<xs:restricti on base="xs:intege r">

<xs:maxExclusiv e 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:myidt ype" 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.publi c.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>Whate ver</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:intege r" 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:simpleTyp e name="type1">
<xs:restricti on base="xs:intege r">
<xs:maxInclusiv e 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********@sit esystems.com> wrote in message
news:eY******** ******@TK2MSFTN GP14.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>Whate ver</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:intege r" 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2722
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a namespace prefix, what is its namespace, if default namespaces do not apply? Are (either of) prefixed or non-prefixed attributes correct? For example, are the following equivalent: <html:br class="foo"...
3
4846
by: ruthless | last post by:
hello. I've got a problem - can I do any restrictions for my attributes? I found how to restrict elements and was trying to use them with my attributes e.g. <person sex="F"> sex can be anly F and M i've got sth like this:
2
1766
by: Victor | last post by:
I have a Schema with some elements which are "types" or objects, and some elements which correspond to instances of the types. How can I specify that a <locationInstance> must have an attribute "type" which equals any one of the "id" attributes of the <locationType>s? -- Victor CERN / NTNU
6
2589
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a schema for it with Visual Studio, I get the error "Failed to create a schema for this data file because:
4
2037
by: Mike Jansen | last post by:
Does anyone know why if I create a complexType based off another complexType using xsd:extension the attributes don't seem to be inherited? Is this a bug/non-implementation in the .NET Schema editor and the XML validator? Thanks, Mike Jansen
0
1330
by: ruediger | last post by:
Hi there, I want to set up an XML Schema for documents of the following structure: The <root> node contains - an optional <message> element with a text attribute, and - arbitrary further content. To make the parts unique, the top-level element of the further content
1
2209
by: Robert Mago | last post by:
I have an xml document instance in C# which loads an xml schema. I would like to load the restrictions for a complex type in the schema so that the user will choose one, from a drop down list for example. Any ideas? A stripped down schema segment is shown below: <xs:complexType name="condition">
2
2326
by: mnickel67 | last post by:
Hi, I'm still learning, so apologies if this is trivial... Given the following XML snippet: <Record> <foo id="a">... </foo> <foo id="b">....</foo> <foo id="c">.....</foo>
3
1677
by: Marc Hebert | last post by:
I'm trying to figure out if something is possible to specify and constrain using a schema. I'll explain using an example. sample xml: <person name="John" age="32"/> <person name="Julie" age="25"/> In my schema, I'd love to be able to place different restrictions on the attribute 'age' based on the value of 'name'... so in this example, I might want to enforce that any entry where name="John"
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.