关闭XML Schema上的简单内容标记

时间:2014-09-10 04:22:14

标签: xml xsd

我在再次验证XML文件XML架构时遇到问题。有帮助吗?

链接:http://www.utilities-online.info/xsdvalidation/?save=f640c556-1da8-4cc4-a50c-b72f9d7a8780-xsdvalidation

XSD文件:

<?xml version="1.0"?>

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

<xsd:element name="asdasdasdasd">
    <xsd:complexType>
    <xsd:choice minOccurs="0" maxOccurs="unbounded">


<xsd:element name="student">
    <xsd:complexType>
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="firstname">
            <xsd:simpleContent>
                <xsd:restriction base="xsd:string"/>
                </xsd:restriction>
            </xsd:simpleContent>
        </xsd:element>
        <xsd:element name="lastname">
            <xsd:simpleContent>
                <xsd:restriction base="xsd:string"/>
                </xsd:restriction>
            </xsd:simpleContent>
        </xsd:element>      
            <xsd:element name="email">
            <xsd:simpleContent>
                <xsd:restriction base="xsd:string"/>
                </xsd:restriction>
            </xsd:simpleContent>
        </xsd:element>      
        <xsd:element name="dateofbirth">
            <xsd:simpleContent>
                <xsd:restriction base="xsd:date"/>
                </xsd:restriction>
            </xsd:simpleContent>
        </xsd:element>      
        <xsd:element name="major">
            <xsd:simpleType>
            <xsd:simpleContent>
                <xsd:restriction base="xsd:string"/>
                    <xsd:enumeration value="Computer Science/SD" />
                    <xsd:enumeration value="Computer Science/IT" />
                    <xsd:enumeration value="Math" />
                </xsd:restriction>
            </xsd:simpleContent>
            </xsd:simpleType>
        </xsd:element>
        <xsd:attribute name="stuid" type="xsd:integer"/>
    </xsd:complexType>
    </xsd:choice>
</xsd:element>



    </xsd:complexType>
    </xsd:choice>
</xsd:element>



</xsd:schema>

XML文件:

    <?xml version="1.0"?>
  <asdasd>


    <student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://unixweb.kutztown.edu/~agyaw792/480/hw2xsd.xsd" stuid="002">

             <firstname>John</firstname>
             <lastname>Doe</lastname>
             <email>jdoe619@live.kutztown.edu</email>
             <dateofbirth>2002-09-24</dateofbirth>
             <major>Computer Science/SD</major>

             </student>

             <student stuid="007">
             <firstname>Mary</firstname>
             <lastname>Smith</lastname>
             <email>msmit789@live.kutztown.edu</email>
             <dateofbirth>2004-02-32</dateofbirth>
             <major>Computer Science/IT</major>
             </student>

              <student stuid="123">
             <firstname>Harry</firstname>
             <lastname>Potter</lastname>
             <email>hpott455@live.kutztown.edu</email>
             <dateofbirth>2004-02-12</dateofbirth>
             <major>Math</major>
             </student>

             </asdasdasdasd>    

我对这些东西很陌生,所以我可能会在这里做些傻事。提前道歉。

1 个答案:

答案 0 :(得分:0)

使用体面的xml编辑器或带有xml帮助的IDE,你会在这里发现错误:

<xsd:simpleContent>
    <xsd:restriction base="xsd:string" />   // closed
</xsd:restriction>                    // closing tag with no starting tag
</xsd:simpleContent>

您不必要地关闭限制的开始标记。删除结束标记或删除起始标记中的斜杠。 所有你的元素也有同样的限制。你在他们所有人身上都做了同样的事情

但另一方面,为什么要有限制。只需将firstName的类型设置为xsd:string即可。为什么在这种情况下需要限制?使用相同模式的其余元素也是如此。

此外,拥有一堆匿名类型并不是很好的做法。考虑在外面声明

发现其他问题:

你的学生id属性是学生元素的一个属性,所以应该去外面选择

       <xsd:attribute name="stuid" type="xsd:integer"/>
   </xsd:complexType>
</xsd:choice>

simpleContent内不需要simpleType。拿出来。

<xsd:element name="major">
    <xsd:simpleType>
        <xsd:simpleContent>
            <xsd:restriction base="xsd:string"/>

这是xsd的完整重构,使其有效

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

    <xsd:annotation>
        <xsd:documentation>
            Author: Ankit Gyawalient
            Creation Date: September
            8th, 2014
            Due Date: September 10th, 2014
            Course: CSC 480 010
            Professor
            Name: Dr. Lisa Frye
            Assignment: 2
            Filename: hw2xsd.xsd
            Purpose: To
            define XML Schema for a university in an external file and use it
            appropriately.
        </xsd:documentation>
    </xsd:annotation>

    <xsd:element name="asdasdasdasd">
        <xsd:complexType>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element name="student">
                    <xsd:complexType>
                        <xsd:choice minOccurs="0" maxOccurs="unbounded">
                            <xsd:element name="firstname" type="xsd:string" />
                            <xsd:element name="lastname" type="xsd:string" />
                            <xsd:element name="email" type="xsd:string" />
                            <xsd:element name="dateofbirth" type="xsd:date" />
                            <xsd:element name="major">
                                <xsd:simpleType>
                                    <xsd:restriction base="xsd:string">
                                        <xsd:enumeration value="Computer Science/SD" />
                                        <xsd:enumeration value="Computer Science/IT" />
                                        <xsd:enumeration value="Math" />
                                    </xsd:restriction>
                                </xsd:simpleType>
                            </xsd:element>
                        </xsd:choice>
                        <xsd:attribute name="stuid" type="xsd:integer" />
                    </xsd:complexType>
                </xsd:element>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

最后在你的xml文件中你有这个

<dateofbirth>2004-02-32</dateofbirth>

抱歉没有2月32日这样的日期。至少那个xml是关注的: - )

以上更改将通过。作为旁注,请查看this tutorial以获取更好的参考指南