Connecting Tech Pros Worldwide Forums | Help | Site Map

Either element or attribute in XSD

gisleyt@gmail.com
Guest
 
Posts: n/a
#1: Apr 25 '06
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-


George Bina
Guest
 
Posts: n/a
#2: Apr 25 '06

re: Either element or attribute in XSD


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

Stan Kitsis [MSFT]
Guest
 
Posts: n/a
#3: Apr 25 '06

re: Either element or attribute in XSD


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

<gisleyt@gmail.com> wrote in message
news:1145951114.632432.310300@t31g2000cwb.googlegr oups.com...[color=blue]
> 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-
>[/color]


Closed Thread