XML 中的递归元素 [英] Recursive element in XML

查看:20
本文介绍了XML 中的递归元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 XML 模式中设计和实现一个递归元素,但我对 XML 总体上不太擅长.关于如何设计它的任何想法?

I am trying to design and implement a recursive element in an XML schema, but I'm not very good with XML in general. Any ideas on how to design it?

推荐答案

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

The model below is based on an authoring style where the element declaration is global and the recursiveness is achieved through referencing the element definition.

<?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>

或者,您可以通过重用类型来实现相同的效果:

Alternatively, you may achieve the same by re-using a type:

<?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>

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

Or you can go somewhere in between:

<?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>

这篇关于XML 中的递归元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆