Xml schema element with attribute and optional simple content?

G

Gazza

I want to define an element type which has child elements which will
have an attribute, but I want the presence of content for these child
elements (which will be simple when present) to be optional. My type is
defined as such

<xsd:complexType name="DistanceStructure">
<xsd:sequence>
<xsd:element name="Value">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="Comment" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="Unit">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="esdalcomm:DistanceUnitType">
<xsd:attribute name="Comment" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
This definition allows validation of the following when it appears in
my instance document


<StructureLength>
<Value Comment="STRUCTURE LENGTH COMMENT">1.1</Value>
<Unit Comment="STRUCTURE LENGTH COMMENT">metre</Unit>
</StructureLength>

But the following does not validate

<StructureLength>
<Value Comment="STRUCTURE LENGTH COMMENT"/>
<Unit Comment="STRUCTURE LENGTH COMMENT"/>
</StructureLength>



I need both to be valid possiblilies in my instance document. What can
I do?
 
H

Henry S. Thompson

Gazza said:
<xsd:element name="Value">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="Comment" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<Value Comment="STRUCTURE LENGTH COMMENT">1.1</Value>
<Value Comment="STRUCTURE LENGTH COMMENT"/>

The empty string is not a valid xs:decimal, so you should define a union
of the empty string (xs:string restricted to length 0, or xs:string
restricted to an enumeration of only "") and xs:decimal, and use that
instead of xs:decimal itself.

ht
--
Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
Half-time member of W3C Team
2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
Fax: (44) 131 650-4587, e-mail: (e-mail address removed)
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top