473,834 Members | 1,418 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 9239
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
2726
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
1767
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
2597
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
2039
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
2211
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
2329
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
1681
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
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
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
10790
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
10505
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...
1
10545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9329
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7755
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.