【问题标题】:How to connect XML and XSD and give errors if XML not valid?如果 XML 无效,如何连接 XML 和 XSD 并给出错误?
【发布时间】:2016-01-27 08:08:52
【问题描述】:

我正在尝试使用 xsd 文件验证 XML 文件。我使用

链接 XML 和 XSD
schemaLocation

但 XML 不读取 XSD 文件。

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="one.xsd">
    <person>
        <full_name>Hege Refsnes</full_name>
        <child_name>Cecilie</child_name>
    </person>
    <person>
        <full_name>Tove Refsnes</full_name>
        <child_name>Hege</child_name>
        <child_name>Stale</child_name>
        <child_name>Jim</child_name>
        <child_name>Borge</child_name>
    </person>
    <person>
        <full_name>Stale Refsnes</full_name>
    </person>
</persons>

XSD 文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name="persons">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="person" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="child_name" type="xs:string"
                                        minOccurs="1" maxOccurs="2"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

如果 XML 检测到这个 XSD 文件,它应该返回一个错误,(因为我使用了“name”而不是“full_name”,所以我也限制了“child_name”的 min - 1 和 max - 2)

谁能解释一下如何链接 XML 和 XSD?

【问题讨论】:

  • 你是用什么来读取XML文档的,能否提供源代码?

标签: xml xsd xsd-validation


【解决方案1】:

注意xsi:schemaLocationxsi:noNamespaceSchemaLocation 之间的区别:

  • 当您的 XML 不在中时使用 xsi:noNamespaceSchemaLocation 命名空间
  • 当您的 XML 位于 命名空间 中时,请使用 xsi:schemaLocation

不在命名空间中

您的 XML 不在命名空间中,因此您应该使用 xsi:noNamespaceSchemaLocation:

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

在命名空间中

如果您的 XML 在命名空间中,那么您将使用 xsi:schemaLocation:

<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://example.com/person one.xsd"
         xmlns="http://example.com/person">

(注意命名空间和 XSD 文件名之间的空格;允许附加命名空间 URI 和 XSD 文件名对。)

您的 XSD 将使用 targetNamespace:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           targetNamespace="http://example.com/person" >

【讨论】:

  • 我已经按照你说的修改了 XML 和 XSD,但是它不起作用。我做了什么,我试图在 XAMPP 服务器中运行它。例如:localhost:8888/myfiles/test.xml 然后它返回“此页面包含以下错误:第 2 行第 52 列的错误:xmlns: 'example.com/person one.xsd' 不是有效的 URI 下面是页面的呈现直到第一个错误”错误..
  • 我修复了 In a namespace 示例中的一个错误,但无论如何您都不应该使用该示例。使用 不在命名空间中 示例,因为您的 XML 未使用命名空间:将 xsi:noNamespaceSchemaLocation="one.xsd" 添加到您的 person 根元素而不是 xsi:schemaLocation="one.xsd"
猜你喜欢
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多