Xml schema element with attribute and optional simple content?

Gazza (archive)

Usenet Archive
Joined
Jul 23, 2002
Messages
26
Reaction score
0
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?
 

Henry S. Thompson

Usenet Archive
Joined
Jan 14, 2000
Messages
309
Reaction score
0
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
 

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

No members online now.

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top