Need Simple Element with Attribute & Restriction

This should cover W3C XML Schema, Relax NG and DTD related problems.
husker
Posts: 5
Joined: Wed Nov 28, 2007 7:45 pm
Location: Omaha, Nebraska

Need Simple Element with Attribute & Restriction

Post by husker »

I'm trying to crete a schema definition for a simple data type that has both an attribute & a restriction. For example, in the XML instance document, the element might look like this:

Code: Select all

<speed units="kilometers-per-hour">100</speed>
I tried to create a schema element like this, but OXygen says its's invalid. Can ayone see why?

Code: Select all


<xsd:simpleType name="Speed">
<xsd:restriction base="xsd:nonNegativeInteger" >
<xsd:maxInclusive value="999"/>
</xsd:restriction>
<xsd:attribute name="units" default="miles-per-hour"/>
</xsd:simpleType>
:roll
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

W3C XML Schema does not allow an attribute in a simple type or a restriction of a simple type inside a complex type. You have to define the restriction in a separate simple type and extend it with the attribute in a new complex type.

Code: Select all


    <xsd:simpleType name="SpeedValuesType">
<xsd:restriction base="xsd:nonNegativeInteger">
<xsd:maxInclusive value="999"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="SpeedType">
<xsd:simpleContent>
<xsd:extension base="SpeedValuesType">
<xsd:attribute name="units" default="miles-per-hour"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Regards,.
Sorin
husker
Posts: 5
Joined: Wed Nov 28, 2007 7:45 pm
Location: Omaha, Nebraska

Post by husker »

That did it! Things that should be simple never are!
Zearin
Posts: 107
Joined: Mon Jul 30, 2007 11:31 pm
Location: College Park, MD, United States

Things that should be simple

Post by Zearin »

If you're looking for simple, than I recommend ditching XML Schema. :)

Relax NG is much more writable AND readable, and Relax NG Compact even moreso. Take a look here and see how easy it is to read these schemas:http://relaxng.org/compact-tutorial-20030326.html

And no functionality is lost. Using Oxygen's Trang converter you can write and read schema definitions in the way-simpler Relax NG Compact and convert them to XSD for validation and compatibility with other XML tools.

Good luck! :)

-- Zearin
-- Zearin
Post Reply