我的术语可能不正确,因为我是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 票数:2 已采纳

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

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

  ask by Matthew Hoggan translate from so

未解决问题?本站智能推荐: