473,769 Members | 8,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="character istics">
<xs:complexType >
<xs:sequence>
<xs:element name="character istic">
<xs:complexType >
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unbo unded">
<xs:complexType >
<xs:attribute name="value" type="xs:string "
use="required" />
<xs:attribute name="descripti on" type="xs:string "
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKE N"
use="optional" />
<xs:attribute name="values" type="xs:string "
use="optional" />
<xs:attribute name="column" type="xs:NMTOKE N"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

Mar 7 '07 #1
4 2709
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
<characteristic element (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******** *************@h 3g2000cwc.googl egroups.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="character istics">
<xs:complexType >
<xs:sequence>
<xs:element name="character istic">
<xs:complexType >
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unbo unded">
<xs:complexType >
<xs:attribute name="value" type="xs:string "
use="required" />
<xs:attribute name="descripti on" type="xs:string "
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKE N"
use="optional" />
<xs:attribute name="values" type="xs:string "
use="optional" />
<xs:attribute name="column" type="xs:NMTOKE N"
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:uniquedoe s 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:keyinstea d 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***@microsof t.comwrote in message
news:45******** @news.microsoft .com...
You can do what you want by adding the following uniquness constraint to
the <characteristic element (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******** *************@h 3g2000cwc.googl egroups.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
"characteristi c" with different content, but this is clearly not
allowed in XSD.

Does anyone have an answer? Thanks.

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

<xs:element name="character istics">
<xs:complexType >
<xs:sequence>
<xs:element name="character istic">
<xs:complexType >
<xs:sequence>
<xs:element name="value" minOccurs="0"
maxOccurs="unb ounded">
<xs:complexType >
<xs:attribute name="value" type="xs:string "
use="require d" />
<xs:attribute name="descripti on" type="xs:string "
use="optiona l" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKE N"
use="optiona l" />
<xs:attribute name="values" type="xs:string "
use="optiona l" />
<xs:attribute name="column" type="xs:NMTOKE N"
use="require d" />
</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
1748
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 allow me to do this? I tried running XSD on the entire assembly, with the plan to delete the unnecessary classes from the .xsd afterwords. However, it complained that a certain class "cannot be serialized because it does not have a default...
0
2429
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 service. I have run the schema through XML Spy and validated it with no errors, but when attempting to generate a dataset, I keeping getting a 'undefined data type' error. The schema is quite large so I don't know if that is causing the problem or...
2
3801
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( _PathToXMLDoc); XmlValidatingReader valReader = new XmlValidatingReader(xmlReader); valReader.ValidationType = ValidationType.Schema;
2
10758
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 xml files, VS.NET still reports that no schema could be located. Here are the relevant (maybe) portions of the files: FROM THE SCHEMA FILE (mydoc-v2.xsd) <?xml version="1.0" encoding="utf-8" ?> <xs:schema...
2
1499
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 file: http://www.w3.org/MarkUp/SCHEMA/xhtml-notations-1.xsd The problem seems to be on this line: <xs:notation name="cdata" public="-//W3C//NOTATION XML 1.0: CDATA//EN" />
3
2032
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> </xsd:annotation> </xsd:attributeGroup> </xsd:extension>
3
4440
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 created to the dataset so that when I do use the "WriteXml" method, the generated xml file will be properly formatted to the schema I created. I did try using the "ReadXMLSchema" but it didn't work. Please help me!
2
1628
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> <Data1 10 </Data1> <Data2 20 </Data2>
3
1679
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: http://emle.sourceforge.net/emle020000/emle_lab.xsl transform: http://emle.sourceforge.net/emle020000/emle_lab_003.xml . The results display as I expect and look fine to me using the "DOM Inspector". I have also used CookTop V2.5 to validate the XML and to run the...
1
8126
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 while keeping the same namespace. This works fine, until I try to introduce the schema. My input looks like this: <a xmlns="some_url"> <b>doo dah</b>
0
10219
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...
1
9998
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
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
8876
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
7413
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
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();...
1
3967
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
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.