473,387 Members | 1,453 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,387 software developers and data experts.

XML Schema (XSD): allow an attribute OR a sub-element NOT BOTH

Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.

I tried using a <choicewith two element definitions for
"characteristic" with different content, but this is clearly not
allowed in XSD.

Does anyone have an answer? Thanks.

================================================== ======================

<xs:element name="characteristics">
<xs:complexType>
<xs:sequence>
<xs:element name="characteristic">
<xs:complexType>
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="value" type="xs:string"
use="required" />
<xs:attribute name="description" type="xs:string"
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN"
use="optional" />
<xs:attribute name="values" type="xs:string"
use="optional" />
<xs:attribute name="column" type="xs:NMTOKEN"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

Mar 7 '07 #1
4 2688
On 7 Mar, 18:46, "Eric" <eml...@hotmail.comwrote:
Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.

I tried using a <choicewith two element definitions for
"characteristic" with different content, but this is clearly not
allowed in XSD.
Alas this is not possible with XML schema 1.0. XSD 1.1 is expected to
address this as it is a common requirement, but there's no timetable
for that that I know of.

Some people embed schematron constraints into their schemas to address
this, but I don't know how good the tool support for this is.

Relax-NG should also be able to support this, but again tool support
may be an issue.

HTH,

Pete.
--
=============================================
Pete Cordell
Tech-Know-Ware Ltd
for XML to C++ data binding visit
http://www.tech-know-ware.com/lmx/
http://www.codalogic.com/lmx/
=============================================

Mar 7 '07 #2
The other current solution is to name the two versions differently and
make the content be a choice of one or the other of them. That's more of
a pain for the document and application authors, but it does accomplish
the goal.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 7 '07 #3
You can do what you want by adding the following uniquness constraint to the
<characteristicelement (before </xs:element>):

<xs:unique name="value_or_values">
<xs:selector xpath="."/>

<xs:field xpath="@values|mytns:value"/>

</xs:unique>

Note, this assumes that you have defined a target namespace for your schema
and its prefix is mytns. If you don't have the target namespace defined,
you will need to define it (and assign a prefix to it).

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Eric" <em****@hotmail.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.

I tried using a <choicewith two element definitions for
"characteristic" with different content, but this is clearly not
allowed in XSD.

Does anyone have an answer? Thanks.

================================================== ======================

<xs:element name="characteristics">
<xs:complexType>
<xs:sequence>
<xs:element name="characteristic">
<xs:complexType>
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="value" type="xs:string"
use="required" />
<xs:attribute name="description" type="xs:string"
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN"
use="optional" />
<xs:attribute name="values" type="xs:string"
use="optional" />
<xs:attribute name="column" type="xs:NMTOKEN"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

Mar 7 '07 #4
One thing I forgot to mention is that <xs:uniquedoes not require you to
have at least one. It says that you can have one or the other or none, but
not both. If you want to require either the element or the attribute to be
present, use <xs:keyinstead of <xs:unique- everything else stays the
same.

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Stan Kitsis [MSFT]" <sk***@microsoft.comwrote in message
news:45********@news.microsoft.com...
You can do what you want by adding the following uniquness constraint to
the <characteristicelement (before </xs:element>):

<xs:unique name="value_or_values">
<xs:selector xpath="."/>

<xs:field xpath="@values|mytns:value"/>

</xs:unique>

Note, this assumes that you have defined a target namespace for your
schema and its prefix is mytns. If you don't have the target namespace
defined, you will need to define it (and assign a prefix to it).

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no
rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Eric" <em****@hotmail.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
>Attached is an example of my question. Note the "values" attribute is
optional. Also the <valuesub-element is optional. Here, the XML
can contain, 1 or both or neither. I would like to allow EITHER the
"values" attribute or <valuesub-elements - but not both.

I tried using a <choicewith two element definitions for
"characteristic" with different content, but this is clearly not
allowed in XSD.

Does anyone have an answer? Thanks.

================================================= =======================

<xs:element name="characteristics">
<xs:complexType>
<xs:sequence>
<xs:element name="characteristic">
<xs:complexType>
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="value" type="xs:string"
use="required" />
<xs:attribute name="description" type="xs:string"
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN"
use="optional" />
<xs:attribute name="values" type="xs:string"
use="optional" />
<xs:attribute name="column" type="xs:NMTOKEN"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>


Mar 7 '07 #5

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

Similar topics

0
by: Robert Jacobson | last post by:
Hi, I have a number of classes in one assembly. I'd like to use XSD.exe to generate a schema for just one class ("Parse Rules") and its nested child classes. Is there an attribute that will...
0
by: Jeremy Summers | last post by:
Has anyone run into problems creating typed datasets from complex schemas? I am attempting to create a Typed Dataset in Visual Studio .Net from a .xsd schema file for a industry standard web...
2
by: Ian Emery | last post by:
Hi, I am trying to validate an XML file using the XmlValidatingReader and XmlUrlResolver. The code for which goes like this: // Read the XML file. XmlTextReader xmlReader = new XmlTextReader(...
2
by: Vagabond Software | last post by:
I have translated a DTD to an XML Schema (XSD) file by hand, and the Schema View in Visual Studio .NET 2003 seems to diplay everything properly. However, when I specify the schemalocation in my...
2
by: Mike Bridge | last post by:
Hi- I've created an XHTML extension module which validates correctly using the W3C online schema validator, but fails when I use the .net 1.1 validator. It seems to be choking on an included W3C...
3
by: comic_rage | last post by:
I am trying to get my schema to look like this <xsd:extension base="MyExtension"> <xsd:attributeGroup ref="DocumentAttributes"> <xsd:annotation> <xsd:documentation>Notes</xsd:documentation>...
3
by: Eric | last post by:
Help! I created a XML schema with a Visual Studio tools. I'm filling a dataset with a DataAdapter. Before I use the "WriteXml" method to write the data to a xml file, I want to map the XSD file I...
2
by: martin1 | last post by:
Hi, All, I want to add xml validation schema namespace attribute for AutoData root node(xsi:noNamespaceSchemaLocation="C:\AutoDataSchema.xsd")in the following file <AutoData > <Table1>...
3
by: C.W.Holeman II | last post by:
I am new to using XML. I am using Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1. In my test I am attempting to have the style sheet:...
1
by: Himself | last post by:
I apologize if this has been answered before, I couldn't find it. I'm trying to transform XML to XML and specify a schema in the output XML. I am transforming nodes to different local names...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.