XSD架构错误,基类型预计是一个复杂的类型,我该如何修复?

时间:2014-03-24 00:43:45

标签: xml xsd schema

不确定发生了什么但是当我尝试验证我的架构时出现此错误:

  

复杂类型' Tnamedefinition':如果使用,基本类型应该是复杂类型。基本类型' {http://www.w3.org/2001/XMLSchema}字符串'是一个简单的类型。

这是我的xml文件:          

<league year="2014" source="www.ico.org"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="champions.xsd">
    <sources>
        <source metalane="top">
                    <champion name="wukong">
                            <popularity>8.18</popularity>
                            <banrate>0.51</banrate>
                    </champion>
                    <champion name="renekton">          
                            <popularity>24.26</popularity>
                            <banrate>8.69</banrate>
                    </champion>
                    <champion name="riven">
                            <popularity>11.75</popularity>
                            <banrate>12.85</banrate>
                    </champion>     
                    <champion name="drmundo">
                            <popularity>12.11</popularity>
                            <banrate>64.46</banrate>
                    </champion>     
        </source>

        <source metalane="mid">
                <champion name="ziggs">
                        <popularity>15.09</popularity>
                        <banrate>20.56</banrate>
                </champion>     
                <champion name="gragas">
                        <popularity>22.45</popularity>
                        <banrate>25.89</banrate>
                </champion>
                <champion name="kayle">
                        <popularity>13.63</popularity>
                        <banrate>9.37</banrate>
                </champion>
                <champion name="khazix">
                        <popularity>21.71</popularity>
                        <banrate>37.28</banrate>
                </champion>         
        </source>

        <source metalane="marksman">
                <champion name="lucian">
                        <popularity>18.95</popularity>
                        <banrate>1.18</banrate>
                </champion>     
                <champion name="ezreal">
                        <popularity>21.26</popularity>
                        <banrate>0.11</banrate>
                </champion>
                <champion name="draven">
                        <popularity>15.88</popularity>
                        <banrate>5.85</banrate>
                </champion>
                <champion name="jinx">
                        <popularity>34.82</popularity>
                        <banrate>4.98</banrate>
                </champion>     
        </source>
    </sources>
</league>

这是我的xsd架构文件:          

<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <!-- A year is a 4-digit integer, except the year must be >= 1998. -->
    <xs:simpleType name="Tyear">
        <xs:restriction base="xs:integer">
            <xs:pattern value="\d{4}"/>
            <xs:minInclusive value="1998"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- A lane is a string which specifies which location on the map a champion goes -->
    <xs:simpleType name="Tmetalane">
        <xs:restriction base="xs:string">

        </xs:restriction>
    </xs:simpleType>

    <!-- A name is a string which specifies a champions name -->
    <xs:simpleType name="Tname">
        <xs:restriction base="xs:string">

        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="Tperc">
        <xs:restriction base="xs:decimal">    
            <xs:minExclusive value="0.0"/>
            <xs:fractionDigits value="2"/>
        </xs:restriction>    
    </xs:simpleType>

    <!-- A definition associates a metalane with a value.  -->
    <xs:complexType name="Tdefinition">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="metalane" type="Tmetalane"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- A name definition associates a champion with a value.  -->
    <xs:complexType name="Tnamedefinition">
        <xs:complexContent>
            <xs:extension base="xs:string">
                <xs:attribute name="name" type="Tname"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>


    <!-- league
    The root element is *league*.
    It contains a sources element which contains multiple source elements.
    -->
    <xs:element name="league">        
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="sources" maxOccurs="unbounded"/>        
            </xs:sequence>
            <xs:attribute name="year" type="Tyear"/>
            <xs:attribute name="source" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>

    <!-- sources
    The *sources* element defines multiple metalanes which
    contain champion information regarding that lane.
    -->
    <xs:element name="sources">        
        <xs:complexType>
            <xs:sequence>
                <xs:element name="source" type="Tdefinition" maxOccurs="unbounded"/>        
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="metalanes">
            <xs:selector xpath="source"/>
            <xs:field xpath="@metalane"/>
        </xs:unique>
    </xs:element>

    <!-- source
    The *source* element contains multiple champion elements
    -->
    <xs:element name="source">        
        <xs:complexType>
            <xs:sequence>
                <xs:element name="champion" type="Tnamedefinition" maxOccurs="unbounded"/>        
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- champion
    The *champion* element contains a banrate and popularity element
    -->
    <xs:element name="champion">        
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="popularity"/>        
                <xs:element ref="banrate"/>
            </xs:sequence>
            <xs:attribute name="name" type="Tname"/>
        </xs:complexType>
    </xs:element>

    <!-- A *popularity* element defines how often a champion is picked  -->
    <xs:element name="popularity">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="Tperc">
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

    <!-- A *banrate* element defines how often a champion is banned  -->
    <xs:element name="banrate">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="Tperc">
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

</xs:schema>

2 个答案:

答案 0 :(得分:0)

您宣布<complexContent>。您不能使用简单类型(xs:string)作为扩展的基本类型。它必须是一个复杂的类型。

您将<champion>定义为具有匿名嵌套复杂类型的顶级元素,然后再将<source>内的Tnamedefinition定义为champion。从您的XML源我相信您应该实际声明对顶级type的引用并删除<xs:element name="source"> <xs:complexType> <xs:sequence> <xs:element ref="champion" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> 声明:

Tnamedefinition

然后您可以删除<sources>的类型声明,这会导致错误。

<xs:element name="sources"> <xs:complexType> <xs:sequence> <xs:element name="source" type="Tdefinition" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> 中存在同样的问题,因为您将其声明为空,因此无法验证您的文档。

Tdefinition

...

<source>是一种复杂类型,内容简单,只允许属性。您可以将属性声明移动到<xs:element name="source"> <xs:complexType> <xs:sequence> <xs:element ref="champion" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="metalane" type="Tmetalane" /> </xs:complexType> </xs:element> 元素定义中的复杂类型:

Tdefinition

您也不需要source,并且应该声明<xs:element name="sources"> <xs:complexType> <xs:sequence> <xs:element ref="source" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> ... </xs:element> 作为对顶级元素的引用。

popularity

通过这些更改,文档将进行验证。但是你也有一堆未使用的类型声明,除非你的其他XML实例与你在这里显示的实例非常不同,否则你可能不会使用并且可以删除。例如,除非banrate<xs:element name="popularity" type="Tperc"/> <xs:element name="banrate" type="Tperc"/> 具有属性,否则您可以将它们声明为简单类型:

champion

由于您不在其他任何地方使用它们,您也可以将它们嵌套在Tmetalane定义中。

我认为Tname<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:simpleType name="Tyear"> <xs:restriction base="xs:integer"> <xs:pattern value="\d{4}"/> <xs:minInclusive value="1998"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="Tmetalane"> <xs:restriction base="xs:string"> <!-- to be added --> </xs:restriction> </xs:simpleType> <xs:simpleType name="Tname"> <xs:restriction base="xs:string"> <!-- to be added --> </xs:restriction> </xs:simpleType> <xs:simpleType name="Tperc"> <xs:restriction base="xs:decimal"> <xs:minExclusive value="0.0"/> <xs:fractionDigits value="2"/> </xs:restriction> </xs:simpleType> <xs:element name="league"> <xs:complexType> <xs:sequence> <xs:element ref="sources" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="year" type="Tyear"/> <xs:attribute name="source" type="xs:string" use="optional"/> </xs:complexType> </xs:element> <xs:element name="sources"> <xs:complexType> <xs:sequence> <xs:element ref="source" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:unique name="metalanes"> <xs:selector xpath="source"/> <xs:field xpath="@metalane"/> </xs:unique> </xs:element> <xs:element name="source"> <xs:complexType> <xs:sequence> <xs:element ref="champion" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="metalane" type="Tmetalane" /> </xs:complexType> </xs:element> <xs:element name="champion"> <xs:complexType> <xs:sequence> <xs:element name="popularity" type="Tperc"/> <xs:element name="banrate" type="Tperc"/> </xs:sequence> <xs:attribute name="name" type="Tname"/> </xs:complexType> </xs:element> </xs:schema> 是您仍在处理的类型(因为限制已经到位但是为空)。

进行更改并删除未使用的类型,生成的模式如下:

{{1}}

答案 1 :(得分:0)

以下是来自Saxon的消息进行比较:

Error on line 44 of test.xsd:
  The type Tnamedefinition has complex content so it cannot be based on the simple type xs:string

可以通过扩展名从xs:string定义的内容是“具有简单内容的复杂类型”(即具有属性但没有子元素的元素)。只需将“complexContent”更改为“simpleContent”即可实现。