Xml schema element with attribute and optional simple content?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gazza

    Xml schema element with attribute and optional simple content?

    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:complexTyp e name="DistanceS tructure">
    <xsd:sequence >
    <xsd:element name="Value">
    <xsd:complexTyp e>
    <xsd:simpleCont ent>
    <xsd:extensio n base="xsd:decim al">
    <xsd:attribut e name="Comment" type="xsd:strin g"/>
    </xsd:extension>
    </xsd:simpleConte nt>
    </xsd:complexType >
    </xsd:element>
    <xsd:element name="Unit">
    <xsd:complexTyp e>
    <xsd:simpleCont ent>
    <xsd:extensio n base="esdalcomm :DistanceUnitTy pe">
    <xsd:attribut e name="Comment" type="xsd:strin g"/>
    </xsd:extension>
    </xsd:simpleConte nt>
    </xsd:complexType >
    </xsd:element>
    </xsd:sequence>
    </xsd:complexType >
    This definition allows validation of the following when it appears in
    my instance document


    <StructureLengt h>
    <Value Comment="STRUCT URE LENGTH COMMENT">1.1</Value>
    <Unit Comment="STRUCT URE LENGTH COMMENT">metre</Unit>
    </StructureLength >

    But the following does not validate

    <StructureLengt h>
    <Value Comment="STRUCT URE LENGTH COMMENT"/>
    <Unit Comment="STRUCT URE LENGTH COMMENT"/>
    </StructureLength >



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

  • Henry S. Thompson

    #2
    Re: Xml schema element with attribute and optional simple content?

    Gazza writes:
    [color=blue]
    > <xsd:element name="Value">
    > <xsd:complexTyp e>
    > <xsd:simpleCont ent>
    > <xsd:extensio n base="xsd:decim al">
    > <xsd:attribut e name="Comment" type="xsd:strin g"/>
    > </xsd:extension>
    > </xsd:simpleConte nt>
    > </xsd:complexType >
    > </xsd:element>[/color]
    [color=blue]
    > <Value Comment="STRUCT URE LENGTH COMMENT">1.1</Value>[/color]
    [color=blue]
    > <Value Comment="STRUCT URE LENGTH COMMENT"/>[/color]

    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: ht@inf.ed.ac.uk
    URL: http://www.ltg.ed.ac.uk/~ht/
    [mail really from me _always_ has this .sig -- mail without it is forged spam]

    Comment

    Working...