XML中的递归元素

时间:2012-09-26 08:34:45

标签: xml xsd

我正在尝试在XML模式中设计和实现递归元素,但我对XML一般不太熟悉。关于如何设计它的任何想法?

1 个答案:

答案 0 :(得分:4)

下面的模型基于一种创作风格,其中元素声明是全局的,并且通过引用元素定义来实现递归。

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="recursive">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="recursive" minOccurs="0"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

或者,您可以通过重复使用类型来实现相同目的:

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="recursive" type="Trecursive"/>
    <xsd:complexType name="Trecursive">
        <xsd:sequence>
            <xsd:element name="recursive" type="Trecursive" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

或者你可以介于两者之间:

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="recursive" type="Trecursive"/>
    <xsd:complexType name="Trecursive">
        <xsd:sequence>
            <xsd:element ref="recursive" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

有效样本XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<recursive xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <recursive>
        <recursive/>
    </recursive>
</recursive>