XML Schema question: A <xs:choice> equivalent for attributes?

View: New views
4 Messages — Rating Filter:   Alert me  

XML Schema question: A <xs:choice> equivalent for attributes?

by youhaodeyi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I wish to create an XML document in which one of its elements must be
 defined with one of two known attributes, but not both.

 For example, I would like to define a "socketHandler" element as follows:

 <xs:element name="socketHandler">

     <xs:complexType>

         <xs:attribute name="hostName" type="xs:string"/>

         <xs:attribute name="hostAddress" type="xs:string"/>

         <xs:attribute name="port" type="xs:int" use="required"/>

     </xs:complexType>

 </xs:element>

 but I only want a user to be able to define either a hostName attribute
 (e.g., hostName="<servername>") or a hostAddress (e.g.,
 hostAddress="<IP_address>") attribute, but not both.  It appears that the
 <xs:choice> construct accomplishes this for elements.  Is there a functional
 equivalent for attributes, or is there a better way to approach this?

RE: XML Schema question: A <xs:choice> equivalent for attributes?

by Michael Kay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


It's a well-known limitation of XSD 1.0 that you can't do this.

It's easy in XSD 1.1, but most processors don't support that yet.  (Saxon
does support a sufficient subset, however).

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 




RE: XML Schema question: A <xs:choice> equivalent for attributes?

by Simon Cox :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Else just use elements.
Why does XML have the choice of element or attribute, anyway ;-)


--------------------------------------------------------
Simon Cox

European Commission, Joint Research Centre,
Institute for Environment and Sustainability,
Spatial Data Infrastructures Unit, TP 262
Via E. Fermi, 2749, I-21027 Ispra (VA), Italy
Tel: +39 0332 78 3652
Fax: +39 0332 78 6325
mailto:simon.cox@...
http://ies.jrc.ec.europa.eu/simon-cox 

SDI Unit: http://sdi.jrc.ec.europa.eu/ 
IES Institute: http://ies.jrc.ec.europa.eu/
JRC: http://www.jrc.ec.europa.eu/
--------------------------------------------------------

-----Original Message-----
From: xmlschema-dev-request@... [mailto:xmlschema-dev-request@...] On
Behalf Of Michael Kay
Sent: Friday, 28 August 2009 12:22
To: 'youhaodeyi'; xmlschema-dev@...
Subject: RE: XML Schema question: A <xs:choice> equivalent for attributes?


It's a well-known limitation of XSD 1.0 that you can't do this.

It's easy in XSD 1.1, but most processors don't support that yet.  (Saxon
does support a sufficient subset, however).

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 





RE: XML Schema question: A <xs:choice> equivalent for attributes?

by G. Ken Holman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At 2009-08-28 15:13 +0200, Simon Cox wrote:
>Else just use elements.
>Why does XML have the choice of element or attribute, anyway ;-)

Of course you can coerce any attribute value into an element, but I
like having the distinction between containment and attachment.  Thus
there is no problem distinguishing that which is content of an
element from that which is about the element as a whole.  When some
child elements are used to talk about the element as a whole and some
child elements are used for information contained by the element,
processing the information may not be different but understanding the
relationships between information may be very different.

Of course if you want a structured attribute you are obliged to
coerce that information into a child element (a good example here is
<xsl:sort>).  So they aren't symmetrical in that regard.

And then not all schema languages treat attributes as first-class
constructs like elements (RELAX-NG is good in doing so, for example,
modeling a choice between an element and an attribute, or basing the
complete structure of an element on different attribute values, or
from the original post declaring an exclusive choice between
attributes).  So if you can't use RELAX-NG then there are restrictions.

Robin Cover has collected various opinions about this:

   http://xml.coverpages.org/elementsAndAttrs.html

There is no "correct way of thinking" in this regard .... horses for
courses ... and I still find some students who shy away completely
from attributes and think everything should be done as elements.

I think it is an important distinction.

. . . . . . . . . . . . Ken


--
Interested in these classes?  http://www.CraneSoftwrights.com/x/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/x/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@...
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/x/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal