XML Schema:允许复杂类型上的一系列元素和属性

时间:2014-04-21 02:46:50

标签: xml xsd

我的术语可能不对,因为我不熟悉XML。

我提出了这种XML格式:

<?xml version="1.1" encoding="UTF-8"?>

<?xml-model href="RuleBuilder.xsd" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<root>
    <RuleBuilder:game_system
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="file:///./RuleBuilder.xsd RuleBuilder.xsd"
        xmlns:RuleBuilder="file:///./RuleBuilder.xsd">
        <RuleBuilder:rule_book>
            <RuleBuilder:title>"Warhammer The Game of Fantasy Battles"</RuleBuilder:title>
            <RuleBuilder:page_count>100</RuleBuilder:page_count>
            <RuleBuilder:rule>
                <RuleBuilder:id>0</RuleBuilder:id>
                <RuleBuilder:rule_type>"character"</RuleBuilder:rule_type>
                <RuleBuilder:title>"Monsterous Infantry"</RuleBuilder:title>
                <RuleBuilder:description>"This is a test"</RuleBuilder:description>
                <RuleBuilder:page_begin>200</RuleBuilder:page_begin>
                <RuleBuilder:related_definition>0</RuleBuilder:related_definition>
            </RuleBuilder:rule>
            <RuleBuilder:definition>
                <RuleBuilder:id>0</RuleBuilder:id>
                <RuleBuilder:term>"Monsterous Infantry"</RuleBuilder:term>
                <RuleBuilder:definition>"A big bertha"</RuleBuilder:definition>
                <RuleBuilder:page_begin>200</RuleBuilder:page_begin>
                <RuleBuilder:related_rule>0</RuleBuilder:related_rule>
            </RuleBuilder:definition>
        </RuleBuilder:rule_book>
    </RuleBuilder:game_system>
</root>

对于此架构验证正常:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" xmlns="RuleBuilder"
    targetNamespace="RuleBuilder" elementFormDefault="qualified" vc:minVersion="1.1">

    <xs:element name="game_system">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="rule_book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>         
                            <xs:element name="page_count" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="rule" maxOccurs="unbounded">
                                <xs:complexType>          
                                    <xs:sequence>
                                        <xs:element name="id" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="rule_type" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="descrpiption" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="page_begin" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="related_definition" type="xs:nonNegativeInteger"/>
                                        <xs:element name="related_rule" type="xs:nonNegativeInteger"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="definition" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="id" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="term" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="definition" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="page_begin" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
                                        <xs:element name="related_definition" type="xs:nonNegativeInteger"/>
                                        <xs:element name="related_rule" type="xs:nonNegativeInteger"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

问题:我想知道怎么做是修改架构,以便我可以为一些“包装”复杂类型的元素添加属性。例如:

<?xml version="1.1" encoding="UTF-8"?>

...
<root>
    <RuleBuilder:game_system
        <RuleBuilder:rule_book
            title="Warhammer Fantasy"> <!-- Notice that I move the title here -->
            ....
        </RuleBuilder:rule_book>
    </RuleBuilder:game_system>
</root>

1 个答案:

答案 0 :(得分:2)

要添加属性,您只需在xs:attribute元素声明的xs:complexType中添加rule_book声明:

<xs:complexType>
    <xs:sequence>
        <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>         
        <xs:element name="page_count" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
        <xs:element name="rule" maxOccurs="unbounded" type="RuleType"/>
        <xs:element name="definition" maxOccurs="unbounded" type="DefinitionType"/>
    </xs:sequence>
    <xs:attribute name="title" type="xs:string"/>
</xs:complexType>

它仍然无法验证。您必须确定related_definitionrelated_rule元素的规则。声明它们时,您提供的实例将无法验证,因为两个块中都缺少其中一个实例。

在下面的完整模式中(验证您的来源),我向其中添加了minOccurs="0",以便实例验证。我还在元素声明之外放置了类型,以使文件更具可读性。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"
           xmlns="RuleBuilder" targetNamespace="RuleBuilder" elementFormDefault="qualified" >

    <xs:element name="game_system">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="rule_book" maxOccurs="unbounded" type="RuleBookType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="RuleBookType">
        <xs:sequence>
            <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>         
            <xs:element name="page_count" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
            <xs:element name="rule" maxOccurs="unbounded" type="RuleType"/>
            <xs:element name="definition" maxOccurs="unbounded" type="DefinitionType"/>
        </xs:sequence>
        <xs:attribute name="title" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="RuleType">          
        <xs:sequence>
            <xs:element name="id" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
            <xs:element name="rule_type" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="description" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="page_begin" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
            <xs:element name="related_definition" type="xs:nonNegativeInteger" minOccurs="0"/>
            <xs:element name="related_rule" type="xs:nonNegativeInteger" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="DefinitionType">
        <xs:sequence>
            <xs:element name="id" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
            <xs:element name="term" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="definition" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="page_begin" type="xs:nonNegativeInteger" minOccurs="1" maxOccurs="1"/>
            <xs:element name="related_definition" type="xs:nonNegativeInteger" minOccurs="0"/>
            <xs:element name="related_rule" type="xs:nonNegativeInteger" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>