开发者

Nested Data XML design

开发者 https://www.devze.com 2022-12-26 07:09 出处:网络
Looking to nest (to unlimited levels) elements in XML. Like so: <items> <item> <name>Item One</name>

Looking to nest (to unlimited levels) elements in XML. Like so:

<items>
    <item>
        <name>Item One</name>
        <item>
            <name>Item Two</name>
        </item>开发者_运维百科
        <item>
            <name>Item Three</name>
            <item>
                <name>Item Four</name>
            </item> <!-- etc... -->
        </item>
    </item>
</items>

However. While browsing for a solution I noticed in the comments of: weird nesting in xml while the above is well formed it would not validate against any sinsible DTD.

Two things, what is a better way of nesting similar elements, and secondly what would be the design of the DTD.

UPDATE: Would prefer to validate against an XML Schema rather than DTD.


DTD allows you to define recursive structures like this. In fact, XML would be pretty useless if it couldn't handle that.

<!ELEMENT items (item*)>
<!ELEMENT item (name,item*)>
<!ELEMENT name (#PCDATA)>

XSD allows you to do it, too. It's just a bit more writing:

<xsd:element name="items">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="item" type="itemType" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:complexType name="itemType">
  <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="item" type="itemType" minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号