如何正确构建XSD文件?

时间:2016-08-30 22:34:11

标签: xml xsd

我根据 UML XML 代码(下面的链接和代码)构建了一个 XSD 文件,但我无法做到验证XML文件。

<?xml version="1.0" encoding="ISO-8859-15"?>
<Library
xsi:noNamespaceSchemaLocation="lib.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

<authors>
    <author id="author1" name="Einstein"/>
</authors> 
<publications>
    <Book year="1942" title="7 Kingdoms" author="Einstein"/>
    <Magazine year="2010" number="203" title="The News"/> 
    <Book year="1956" title="The Fall" author="author1"/>
</publications> 
</Library> 

以下是我根据 UML XML代码构建的 XSD 文件:

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

    <xsd:element name="library" type="Library">

<!-- Begin KEY -->
    <xsd:key name="Author">
        <xsd:selector xpath="./authors/Author"/>
        <xsd:field xpath="@id"/>
    </xsd:key>

    <xsd:key name="Magazine">
        <xsd:selector xpath="./publications/Magazine"/>
        <xsd:field xpath="@id"/>
    </xsd:key>

    <xsd:key name="Book">
        <xsd:selector xpath="./publications/Book"/>
        <xsd:field xpath="@id"/>
    </xsd:key>

    <xsd:key name="Publication">
        <xsd:selector xpath="./publications/Magazine | ./publications/Book"/>
        <xsd:field xpath="@id"/>
    </xsd:key>
    <!-- END KEY -->

    <!-- Begin KEYREF -->
    <xsd:keyref name="Book.author" refer="Author">
        <xsd:selector xpath="./publications/Book/author"/>
        <xsd:field xpath="@ref"/>
    </xsd:keyref>
    <!-- END KEYREF -->     

    </xsd:element>


    <xsd:complexType name="Author">
        <xsd:attribute name="id" type="xsd:string"/>
        <xsd:attribute name="name" type="xsd:string"/>
    </xsd:complexType>

    <xsd:complexType name="Publication">
        <xsd:attribute name="title" type="xsd:string"/>
        <xsd:attribute name="year" type="xsd:integer"/>
    </xsd:complexType>

    <xsd:complexType name="Magazine">
        <xsd:complexContent>
            <xsd:extension base="Publication">
                <xsd:attribute name="number" type="xsd:integer"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

    <xsd:complexType name="Book">
        <xsd:complexContent>
            <xsd:extension base="Publication"/>
        </xsd:complexContent>
    </xsd:complexType>

    <xsd:complexType name="Library">
        <xsd:sequence>
            <xsd:element name="book" type="Book" maxOccurs="unbounded"/>
            <xsd:element name="magazine" type="Magazine" maxOccurs="unbounded"/>
            <xsd:element name="author" type="Author" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

当我尝试验证XML文件时,我遇到了一个问题,因为它没有找到元素&#34; Library&#34;的声明。

我不确定创建的 keyref name =&#34; Book.author&#34; 是否足以建立图书与作者之间的联系,我相信问题来自于此。

我是否需要在 complexType name =&#34; Book&#34; 中添加代码以创建链接,从而验证XML文件?

1 个答案:

答案 0 :(得分:0)

您的架构与XML完全匹配

目前,您的架构所代表的XML是。

<library>
  <book title="title_0" year="100" /> 
  <magazine title="title_0" year="100" number="100" /> 
  <author id="id_0" name="name_1" /> 
</library>

无法验证XML

的原因
  1. 正如CecilWard所说,您的架构使用小写字母定义了库,但您的XML示例将其作为标题大小写(库)。

  2. 此外,您的作者节点未定义为作者节点下的重复项目。

  3. 您尚未定义可以包含书籍或杂志的选择节点。如果书籍或杂志像你的例子XML一样混合在一起,那么无限应该是那本书或杂志。

  4. 我刚刚生成了一个XML模式,这将验证该XML文件。

    它没有您定义的任何keyref,但您可以重新添加。

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Library">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="authors">
              <xs:complexType>
                <xs:sequence>
                  <xs:element maxOccurs="unbounded" name="author">
                    <xs:complexType>
                      <xs:attribute name="id" type="xs:string" use="required" />
                      <xs:attribute name="name" type="xs:string" use="required" />
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="publications">
              <xs:complexType>
                <xs:sequence>
                  <xs:choice maxOccurs="unbounded">
                    <xs:element name="Book">
                      <xs:complexType>
                        <xs:attribute name="year" type="xs:integer" use="required" />
                        <xs:attribute name="title" type="xs:string" use="required" />
                        <xs:attribute name="author" type="xs:string" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="Magazine">
                      <xs:complexType>
                        <xs:attribute name="year" type="xs:integer" use="required" />
                        <xs:attribute name="number" type="xs:integer" use="required" />
                        <xs:attribute name="title" type="xs:string" use="required" />
                      </xs:complexType>
                    </xs:element>
                  </xs:choice>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>