2

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

4

1 回答 1

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>
于 2012-09-26T13:38:54.817 回答