Sign In/My Account | View Cart  
advertisement

Article:
 Architectural Design Patterns for XML Documents
Subject: Elements with only sub-elements
Date: 2003-06-05 08:17:21
From: Kyle Downey
Response to: Elements with only sub-elements

Without hacking around with the schemas, it's hard for me to say for sure, but I believe the problem is in how you decided to do inheritance. You're using a restriction with base any, which means rather than allowing anything, you're taking "any" and reducing it to no content, plus the attribute you named. You want an <xs:extension/> element instead. See my follow-on article; it includes an example of inheritance-by-extension. But I think there's an easier way to do this which doesn't require any inheritance:


<complexType>
<any maxOccurs="unbounded"/>
<attribute name="ContentHeight" type="string"/>
</complexType>


Note that if your intent was to allow any element but wanted to require the ContentHeight attribute on the children rather than the <Content> element, your example would be the way to go, assuming you use <xs:extension/>.


As one more note, unless mixed="true", the default content type is "elementOnly." To be explict you could do this:


<complexType content="elementOnly">
...
</complexType>


but it should be unnecessary.


No Previous Message Previous Message Move up to Parent Message Up Next Message No Next Message


Titles Only Titles Only Newest First
  • Elements with only sub-elements
    2003-06-06 07:19:16 Andres Becerra

    I came to a solution based on posts by different people. I wanted to share the solution in case anyone ran into this problem in the future. Below is the XSD I ended up using:



    <xs:element name="Content">
    <xs:complexType>
    <xs:sequence>
    <xs:any minOccurs="0" maxOccurs="0" />
    </xs:sequence>
    <xs:attribute name="ContentHeight" type="xs:string" />
    </xs:complexType>
    </xs:element>



    Another option I had was to tightly validate exactly what valid elements are allowed in <Content> elements, using this XSD schema snippet:


    <xs:element name="Content">
    <xs:complexType>
    <xs:complexContent>
    <xs:restriction base="xs:anyType">
    <xs:all>
    <xs:element ref="label" minOccurs="0" />
    <xs:element ref="TableView" minOccurs="0" />
    </xs:all>
    <xs:attribute name="ContentHeight" type="xs:string" />
    </xs:restriction>
    </xs:complexContent>
    </xs:complexType>
    </xs:element>



    I ended up going with the 1st option simply because it was more flexible over the long run. In the near future, there may be transformers written for new elements that can be placed in the <Content> element, so I didn't want to restrict it too much.... just as long as no plain text was allowed in the <Content> element, only sub-elements.


    Cheers,
    Andres


Sponsored By: