XML - 允许元素属性

时间:2016-03-09 04:20:04

标签: xml xsd attributes

我正在阅读本书ineasysteps,我正在输入完全相同的代码,但我不断得到同样的错误: “从元素”xsd:attribute“开始,找到了无效的内容。

请让我知道我做错了什么:

这是我的album.xml代码:

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

<!-- XML in easy steps - Page 86. -->

<discography xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation = "album.xsd" >

    <artist>Pink</artist>
    <album id= "2000" >Can't Take Me Home</album>
    <album id= "2001" >Misundaztood</album>
    <album id= "2003" >Try This</album>
    <album id= "2006" >I'm Not Dead</album>
    <album>(...in production)</album>

</discography>

这是我的album.xsd代码:

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

<!-- XML in easy steps - Page 86. -->

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

<xsd:element name="artist" type="xsd:string"/>


<xsd:element name="discography" type="discoType" />
<xsd:element name="album" type="albumType" />


<xsd:complexType name="discoType">
    <xsd:sequence>
        <xsd:element ref="artist"/>
        <xsd:element ref="album" maxOccurs="20" />
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="albumType" >
<xsd:simpleContent>

<xsd:attribute name = "id" type = "xsd:string" />  
<xsd:extension base = "xsd:string" />

</xsd:simpleContent>

</xsd:complexType>

</xsd:schema>

1 个答案:

答案 0 :(得分:1)

<xsd:simpleContent>不能拥有属性。我想你需要这样的东西:

<xsd:attribute name="id"/>
<xsd:complexType name="albumType">
    <xsd:simpleContent>

        <xsd:extension base="xsd:string">
            <xsd:attribute ref="id"/>
        </xsd:extension>

    </xsd:simpleContent>

</xsd:complexType>