473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

xml schema, restrict value of the element by name of the element?

Trying to find out a way how to restrict value of the element by name of an
element,
it might be not even possible, but anyhow.

Let's consider following snippet:

<xs:element name="tag1" type="xs:string" />
<xs:element name="tag2" type="xs:string" />
<xs:element name="tag3" type="xs:string" />
.....
<xs:element name="fields">
<xs:complexType>
<xs:sequence>
<xs:element name="field" type="xs:NMTOKEN" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

what I need is, somehow restrict value of the field based on name of the
elements,
in other word following would be valid
<fields>
<field>tag1</field>
</fields>

but other would give me a validation error.
<fields>
<field>tag1</field>
<field>tag10</field>
</fields>

thank you

Nov 12 '05 #1
1 2195


Maksim wrote:
Trying to find out a way how to restrict value of the element by name of an
element,
it might be not even possible, but anyhow.

Let's consider following snippet:

<xs:element name="tag1" type="xs:string" />
<xs:element name="tag2" type="xs:string" />
<xs:element name="tag3" type="xs:string" />
....
<xs:element name="fields">
<xs:complexType>
<xs:sequence>
<xs:element name="field" type="xs:NMTOKEN" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

what I need is, somehow restrict value of the field based on name of the
elements,
in other word following would be valid
<fields>
<field>tag1</field>
</fields>

but other would give me a validation error.
<fields>
<field>tag1</field>
<field>tag10</field>
</fields>


You might be able to define an enumeration of those tag names e.g.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:simpleType name="fieldNames">
<xs:restriction base="xs:NCName">
<xs:enumeration value="tag1" />
<xs:enumeration value="tag2" />
<xs:enumeration value="tag3" />
</xs:restriction>
</xs:simpleType>

<xs:element name="fields">
<xs:complexType>
<xs:sequence>
<xs:element name="field" maxOccurs="unbounded" type="fieldNames" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

then you can validate instances like

<?xml version="1.0" encoding="UTF-8"?>
<fields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2004102701Xsd.x ml">
<field>tag1</field>
<field>tag2</field>
<field>tag10</field>
</fields>

Note that I have used NCName instead of NMToken as the latter should
only be used for attribute values.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sony Antony | last post by:
Hello: ( Please redirect me to the correct list if this is not where I m supposed to ask this question ) Our application essentially sends xml 'commands' to another system. These commands...
1
by: Fred Smith | last post by:
Any suggestions or tips to the questions below I have been wrestling with would be most welcome: I have an example XSD file I have been experimenting with. Suppose a user can select from 1 to 4...
0
by: Sahatra Kumara | last post by:
I have the schema that was written by other people. This schema seems to be wrong. Altova XML SPY 2005 and .Net have found out the block of schema definition that contains error, namely...
3
by: junlia | last post by:
We are using ACORD xml schema standard, and we need to add to it, so we choose to redefine ACORD xml schema. One of the problems that I ran into is how to add some values to an emumerated list. ...
1
by: Paul Cheevers | last post by:
Hi, I've been trying to write some schema to validate the Value element of an Eq element but to no avail. If the Value Element is off type DateTime then the node should be able to contain text...
3
by: Chris Lieb | last post by:
I am new to XML Schema and am running into a bit of a snag. I have defined an XML-based scripting language for an updater program that I am working on. I would like to make a schema for this...
1
by: brucepickford001 | last post by:
Hi, I have a not simple problem. I want the following XML to be schema valid <PopulationDemographic> <PersonName>Joe Blogs</PersonName> <Age>2</Age> <Category>baby</Category>...
2
by: vardan | last post by:
Hi everyone. I have couple problems with xsd file i'm trying to create. first question. is it possible to restrict year input to current year? I mean i don't want to put <xs:restriction...
3
by: Kai Schlamp | last post by:
Hello! In my schema I have the following: <xs:complexType name="textareaType"> <xs:simpleContent> <xs:restriction base="xs:string"> <xs:attribute ref="label" use="required" />...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.