xml文件如何引用schema 进行有效性验证 或者是我的schema写错了?

浔觅下一站 2020-05-24 01:26:27
books.schema文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="books">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="book" type="bookType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="atitle" type="xs:string"/>
<xs:element name="apublisher" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="translator" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>
<xs:element name="publishDate" type="xs:string"/>
<xs:element name="bookId" type="xs:string"/>
<xs:element name="pages" type="xs:string"/>
<xs:element name="publishNumber" type="xs:string"/>
</xs:sequence>
<xs:attribute name="type">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Foreign"/>
<xs:enumeration value="Chinese"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:schema>

books.xml文件:
<?xml version="1.0"?>

<books>
<book type="Foreign">
<title>SQL Server 2005 从入门到精通(应用技术基础)</title>
<atitle>Microsoft SQL Server 2005 Applied Techniques Step by Step</atitle>
<apublisher>Microsoft Press</apublisher>
<author>(美)Solid Quality Learning</author>
<translator>王为</translator>
<publisher>清华大学出版社</publisher>
<publishDate>2006-9-1</publishDate>
<bookId>7-302-13801</bookId>
<pages>280</pages>
<publishNumber>1-1</publishNumber>
</book>
<book type="Chinese">
<title>数据恢复技术</title>
<author>戴士剑,涂彦辉</author>
<translator>王为</translator>
<publisher>电子工业出版社</publisher>
<publishDate>2005-3-1</publishDate>
<bookId>7-121-00756-8</bookId>
<pages>711</pages>
<publishNumber>2-1</publishNumber>
</book>
</books>


books.xml文件如何引用books.schema文件进行有效性验证?
...全文
138 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
rickylin86 2021-09-14
  • 打赏
  • 举报
回复

Schema文件和XML文件需要做如下修改.需要用带XML有效性验证的程序验证.
book.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.ricky.com/book" targetNamespace="http://www.ricky.com/book" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="books">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="book" type="bookType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="atitle" type="xs:string" minOccurs="0"/>
            <xs:element name="apublisher" type="xs:string" minOccurs="0"/>
            <xs:element name="author" type="xs:string"/>
            <xs:element name="translator" type="xs:string"/>
            <xs:element name="publisher" type="xs:string"/>
            <xs:element name="publishDate" type="xs:string"/>
            <xs:element name="bookId" type="xs:string"/>
            <xs:element name="pages" type="xs:string"/>
            <xs:element name="publishNumber" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="type">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="Foreign"/>
                    <xs:enumeration value="Chinese"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:schema>

books.xml

<?xml version="1.0" encoding="GB2312" standalone="yes"?>
<books
    xmlns="http://www.ricky.com/book"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.ricky.com/book book.xsd">
    <book type="Foreign">
        <title>SQL Server 2005 从入门到精通(应用技术基础)</title>
        <atitle>Microsoft SQL Server 2005 Applied Techniques Step by Step</atitle>
        <apublisher>Microsoft Press</apublisher>
        <author>(美)Solid Quality Learning</author>
        <translator>王为</translator>
        <publisher>清华大学出版社</publisher>
        <publishDate>2006-9-1</publishDate>
        <bookId>7-302-13801</bookId>
        <pages>280</pages>
        <publishNumber>1-1</publishNumber>
    </book>
    <book type="Chinese">
        <title>数据恢复技术</title>
        <author>戴士剑,涂彦辉</author>
        <translator>王为</translator>
        <publisher>电子工业出版社</publisher>
        <publishDate>2005-3-1</publishDate>
        <bookId>7-121-00756-8</bookId>
        <pages>711</pages>
        <publishNumber>2-1</publishNumber>
    </book>
</books>

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧