XML 模式:全部、序列和团体 [英] XML Schema: all, sequence & groups

查看:38
本文介绍了XML 模式:全部、序列和团体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次编写 XML 模式,我找到了一些有用的工具来帮助我编写它.

I'm writing an XML schema for the first time and I found some usefull tools to help me writing it.

现在我处于一个奇怪的境地.我编写的模式对某些工具有效,而对其他一些工具无效.该模式是全部"、序列"和顺序"的混合.和组".这是我的 XML 架构:

Now I'm in a strange situation. The schema I wrote is valid for some tools and not for some others. This schema is a mix of "all", "sequence" and "group". Here is my XML schema:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <xsd:group name="test">

        <xsd:all>
                <xsd:element name="e2" minOccurs="0" maxOccurs="1"/>
                <xsd:element name="e3" minOccurs="0" maxOccurs="1"/>
                <xsd:element name="e4" minOccurs="0" maxOccurs="1"/>
        </xsd:all>
    </xsd:group>

    <xsd:element name="e0">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="e1" maxOccurs="unbounded"/>
                <xsd:group ref="test"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

这个模式对吗?它适用于 这个验证器这个也是,但 Notepad++ 的 XML 工具插件显示无法解析架构文件".

Is this schema right? It goes right with this validator and this one too but the XML Tools plugin for Notepad++ says "Unable to parse schema file".

P.S:我写这个模式是因为我想要一个元素e0"这样就可以混合使用 e1、e2、e3 和 e4.e2、e3 和 e4 应该出现 0 次或 1 次,而 e1 可能出现无限次.例如这个 XML 文件应该通过:

P.S: I wrote this schema because I wanted to have an element "e0" with this the possibility to have a mix of e1, e2, e3 and e4. e2, e3 and e4 should appear 0 or 1 time and e1 could occurs an illimited times. For example this XML files should pass:

<e0>
   <e1/>
   <e1/>
   <e1/>
   <e1/>
   <e1/>
   <e2/>
</e0>

<e0>
   <e2/>
   <e3/>
   <e4/>
</e0>

<e0>
   <e1/>
   <e2/>
   <e3/>
   <e4/>
</e0>

你知道其他方法吗?

谢谢

推荐答案

根据 1.0 版本,您拥有的架构似乎无效 显然这里(入门) 那个

The schema you have seems to be invalid according to the 1.0 version which states plainly here (Primer) that

XML Schema 规定 all 组必须作为唯一的孩子出现在内容模型的顶部.

或者,尝试在此处阅读 XML 架构结构的第 3.8.6 节.在您的列表中,我会添加 .NET 的 XSD 处理器,在您的情况下,它会抱怨:

Alternatively, try to read section 3.8.6 of the XML Schema Structures here. To your list I would add .NET's XSD processor, which in your case will complain as:

'all' 的组引用不是根粒子,或者它被用作扩展.

在 XSD 1.0 中,没有任何解决方案可以用简洁的语法很好地满足您的需求,除非您为 e1 元素(以下称为 e1s)构建包装器.

With XSD 1.0 there is no solution that would give you what you want nicely and with a concised syntax unless you build a wrapper for e1 elements (below as e1s).

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:group name="test">
        <xsd:all>
            <xsd:element name="e1s" minOccurs="0">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="e1" maxOccurs="unbounded"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="e2" minOccurs="0"/>
            <xsd:element name="e3" minOccurs="0"/>
            <xsd:element name="e4" minOccurs="0"/>
        </xsd:all>
    </xsd:group>
    <xsd:element name="e0">
        <xsd:complexType>
            <xsd:group ref="test"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema> 

说到e1元素,必须用e1s包裹

When it comes to e1 elements, they must be wrapped in e1s

<e0>
    <e1s>
        <e1/>
        <e1/>
        <e1/>
        <e1/>
        <e1/>
    </e1s>
    <e2/>
</e0>  

<e0>
    <e1s>
        <e1/>
    </e1s>
    <e2/>
    <e3/>
    <e4/>
</e0>     

然后一切都会验证...

Then it'll all validate...

这篇关于XML 模式:全部、序列和团体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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