X包括模式/命名空间验证? [英] XInclude Schema/Namespace Validation?

查看:14
本文介绍了X包括模式/命名空间验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XML 包含来帮助管理需要供人和机器使用的大型 XML 结构.

I'm trying to use XML Includes to help manage a large XML structure that needs to be usable by both humans and machines.

但是在尝试构建可以针对现有架构进行验证的 XML 文件时遇到了无数问题.这是我正在尝试做的一个简化示例.

But am experiencing a myriad of problems when trying to construct XML files that can be validated against an existing schema. Here's a simplified example of what I'm trying to do.

我的main.xml"文件没有通过验证.

My "main.xml" file does not validate.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Main.xml - This fails to validate. -->
<ns1:main xsi:schemaLocation="http://www.example.com/main main.xsd"
          xmlns:ns1="http://www.example.com/main"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xi="http://www.w3.org/2001/XInclude">

    <name>String</name>
    <xi:include href="child.xml"/> <!-- What I'm trying to do. -->

</ns1:main>

child.xml"文件作为独立文件验证良好.

The "child.xml" file validates fine as a standalone file.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Child.xml - This validates fine, as a standalone file. -->
<ns1:child xsi:schemaLocation="http://www.example.com/main main.xsd"
           xmlns:ns1="http://www.example.com/main"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <name>String</name>
    <age>String</age>

 </ns1:child>

这是我的架构:

 <?xml version="1.0" encoding="UTF-8"?>
 <!-- Main.xsd - My Simplified Schema -->
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:ns1="http://www.example.com/main"
            targetNamespace="http://www.example.com/main">

    <!-- Main Element (References Child) -->
    <xs:element name="main">
         <xs:complexType>
             <xs:sequence>
                 <xs:element name="name" type="xs:string"/>
                 <xs:element ref="ns1:child"/>
             </xs:sequence>
         </xs:complexType>
    </xs:element>

    <!-- Child Element -->
    <xs:element name="child">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="age" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
     </xs:element>

</xs:schema>

我的问题几乎显然与命名空间有关,但我不知道如何解决我的问题.

My issues are almost-obviously related to namespaces but I'm at a loss for how to fix my problem.

推荐答案

正如 skaffman 已经指出的,XML Schema 和 XInclude 不兼容.

As skaffman already pointed out, XML Schema and XInclude are not compatible.

来自 xmllint 的验证错误消息清楚地说明了这一点:

The validation error message from xmllint states this clearly:

main.xml:9: element include: Schemas validity error : Element  '{http://www.w3.org/2001/XInclude}include': This element is not expected. Expected is ( {http://www.example.com/main}child ).
main.xml fails to validate

引用 W3C 建议:XInclude 定义与通过应用 XML 模式生成的扩充信息集.此类扩充信息集可以作为输入信息集提供,或者此类扩充可能应用于包含产生的信息集."

To quote the W3C Recommendation: "XInclude defines no relationship to the augmented infosets produced by applying an XML schema. Such an augmented infoset can be supplied as the input infoset, or such augmentation might be applied to the infoset resulting from the inclusion."

因此,您应该首先通过应用 XIncludes 构建整个 XML 文件,然后验证该文件.

Thus you should first construct the whole XML file by applying the XIncludes, and validate this file afterwards.

您可以使用 xmllint 和 --xinclude 来验证 main.xml.

You can use xmllint with --xinclude to validate main.xml.

这篇关于X包括模式/命名空间验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆