473,405 Members | 2,334 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,405 software developers and data experts.

Either element or attribute in XSD

Is it possibly to specify that I want a either a element or an
attribute to occur, but not both at the same time?

<xs:complexType name="foo">
<xs:sequence>
<xs:element minOccurs="0" ref="bar"/>
</xs:sequence>
<xs:attribute name="bar" default="" type="xs:anyURI"/>
</xs:complexType>

In this complexType I don't want both the element "bar" and the
attribute "bar" to be present, only one of them. Can I express this in
any way in xsd?

-Gisle-

Apr 25 '06 #1
2 2129
Hi,

No, you cannot do that in XML Schema alone. You can do that with
embedded Schematron rules inside XML Schema. Here it is a working
example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo" type="foo">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="bar">
<rule context="foo">
<assert test="count(bar|@bar)=1">Either the element bar or
the attribute bar should be specified.</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="bar" type="xs:anyURI"/>
<xs:complexType name="foo">
<xs:sequence>
<xs:element minOccurs="0" ref="bar"/>
</xs:sequence>
<xs:attribute name="bar" default="" type="xs:anyURI"/>
</xs:complexType>
</xs:schema>

On a document like:

<foo bar="sample">
<bar>sample</bar>
</foo>

The following error will appear:

SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 1:0
Description: Either the element bar or the attribute bar should be
specified. (count(bar|@bar)=1)

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Apr 25 '06 #2
In general, this type of constraint is not available in the Xml Schema.
However, if "bar" is a simple type or a simple content element, your
particular case can be solved with the following XSD:

<xs:element name="top">

<xs:complexType>

<xs:sequence>

<xs:element name="bar" minOccurs="0" type="xs:string"/>

</xs:sequence>

<xs:attribute name="bar" type="xs:anyURI"/>

</xs:complexType>

<xs:unique name="elementORattr">

<xs:selector xpath="."/>

<xs:field xpath="tns:bar|@bar"/>

</xs:unique>

</xs:element>
I used "tns" as the namespace in the xs:field. You'll need to change it to
the prefix you've defined for the target namespace.

--
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

<gi*****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Is it possibly to specify that I want a either a element or an
attribute to occur, but not both at the same time?

<xs:complexType name="foo">
<xs:sequence>
<xs:element minOccurs="0" ref="bar"/>
</xs:sequence>
<xs:attribute name="bar" default="" type="xs:anyURI"/>
</xs:complexType>

In this complexType I don't want both the element "bar" and the
attribute "bar" to be present, only one of them. Can I express this in
any way in xsd?

-Gisle-

Apr 25 '06 #3

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

Similar topics

0
by: Ingrid | last post by:
Am I right in thinking that datatyping at element level ie <xs:element name="num" type="xs:integer"> and specifying a choice of attribute values ie <xs:attribute name="kind"> <xs:simpleType>...
4
by: Gordon Dickens | last post by:
I have target xml to generate from schema. All of the XML instances have the same global element i.e. <base>. I would like to combine all of the schemas into a single schema where I could...
2
by: inquirydog | last post by:
I am trying to store my computer network information in an xml file, and plan to write an xml schema for the file. The general format of the xml should be like this <networkinfo>...
2
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"...
3
by: Anthony Williams | last post by:
Hi all, I'm trying to put together an XSD for an XML type I'm experimenting with. I was just wondering whether it's possible for me to validate whether an image element, which I've defined in...
0
by: magister | last post by:
Hello, I want to have a unique Quesiton Id key for each question under section. Not for every question in the Typed DataSet. Is there anyway I can do this...? Thanks, here is my typed...
6
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...
3
markmcgookin
by: markmcgookin | last post by:
Hi, I have the following XML <AnswerList xmlns="http://tempuri.org/ALPS_Assessmentv1p1_RESCO_Schema.xsd"> <DateTimeLastSaved>12:12:12 1900</DateTimeLastSaved> <UserName>Bob</UserName>...
2
by: mlb5000 | last post by:
I seem to be having issues validating an XML document using my schema. Both are below: The Schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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:
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.