使用多个 xsd 文件验证 xml [英] Validate xml with multiple xsd files

查看:35
本文介绍了使用多个 xsd 文件验证 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Couchdb 创建一个 xml 挂件,并使用 xsd 验证文档.

I am creating an xml pendant to Couchdb, with validation of the documents using xsd.

现在 project:project 的嵌入不起作用并给出验证错误.

Right now the embedding of project:project is not working and gives a validation error.

如何更改我的 xsd,使我的 xml 文件有效?

How can I change my xsd, so my xml file is valid?

我想要以下 xml 布局:

I want the following xml layout:

 <document:document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:document="urn:JCouch.Document"
                   xmlns:attachment="urn:JCouch.Attachment"
                   xmlns:project="urn:JCouch.Project"
                   xsi:schemaLocation="
                   urn:JCouch.Document document.xsd
                   urn:JCouch.Project project.xsd
                   ">
    <document:meta>
        <document:id>123</document:id>
        <document:revision>12321</document:revision>
        <document:deleted>false</document:deleted>
        <document:md5>md5 of everything including</document:md5>
    </document:meta>
    <!-- Auto generated based on files attached -->
    <document:attachments>
        <attachment:attachment>
            <attachment:path></attachment:path>
            <attachment:length>1</attachment:length>
            <attachment:md5></attachment:md5>
        </attachment:attachment>
    </document:attachments>
    <!-- The actual document content -->
    <document:content>
        <project:project>
            <project:name>dsa</project:name>
            <project:projectNumber>123</project:projectNumber>
        </project:project>
    </document:content> 
    </document:document>

我有 3 个 xsd 文件.1个文件,1个附件,1个项目.记住项目只是一个例子.我以后想添加 blog:blog blog:entry 等等.

I have 3 xsd files. 1 for document, 1 for attachment and 1 for project. Have in mind project is just an example. I would later want to add blog:blog blog:entry and so forth.

文档.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       targetNamespace="urn:JCouch.Document"
       xmlns="urn:JCouch.Document"
       xmlns:attachment="urn:JCouch.Attachment"
       xmlns:project="urn:JCouch.Project"
       xsi:schemaLocation="
       urn:JCouch.Attachment attachment.xsd
       urn:JCouch.Project project.xsd"
       elementFormDefault="qualified"
    >

<xs:element name="document" type="documentType"/>

<xs:complexType name="documentType">
    <xs:sequence>
        <xs:element name="meta" minOccurs="1" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element minOccurs="1" maxOccurs="1" name="id" type="xs:string"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="revision" type="xs:string"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="deleted" type="xs:boolean"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="md5" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="attachments" minOccurs="1" maxOccurs="1" type="attachment:attachmentType"/>
        <xs:element name="content" minOccurs="1" maxOccurs="1" type="contentType">
        </xs:element>
    </xs:sequence>
</xs:complexType>


<xs:complexType name="contentType">
    <xs:sequence>
        <xs:element ref="project:project" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>
</xs:schema>

project.xsd:

project.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="urn:JCouch.Project"
       elementFormDefault="qualified">

<xs:complexType name="projectType">
    <xs:sequence>
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="projectNumber" type="xs:string"/>
        </xs:sequence>
    </xs:sequence>
</xs:complexType>

<xs:element name="project" type="projectType"/>

</xs:schema>

最后是attachment.xsd,虽然这部分有效!

and lastly attachment.xsd, though this part works!

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="urn:JCouch.Attachment"
       elementFormDefault="qualified">

<xs:complexType name="attachmentType">
    <xs:sequence>
        <xs:element name="attachment">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="path" type="xs:anyURI"/>
                    <xs:element name="length" type="xs:integer"/>
                    <xs:element name="md5" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>
</xs:schema>

任何帮助将不胜感激.

最好的问候克里斯汀·凯尔

Best regards Kresten Kjaer

推荐答案

由于所有三个模式都有不同的目标命名空间,您可以使用其中之一作为主模式(或驱动程序模式),您将指定给验证器,并且必须在其中导入所有其他模式.

Since all three schemas have different target namespaces, you may use one of them as the main schema (or the driver schema), which you will specify to the validator, and all other schemas must be imported in it.

让我们 Document.xsd 成为主要模式(毕竟,它定义了 XML 的根元素).然后,你可以这样写:

Let's Document.xsd will be the main schema (after all, it defines the root element of your XML). Then, you can write it like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       targetNamespace="urn:JCouch.Document"
       xmlns="urn:JCouch.Document"
       xmlns:attachment="urn:JCouch.Attachment"
       xmlns:project="urn:JCouch.Project"
       xsi:schemaLocation="
       urn:JCouch.Attachment attachment.xsd
       urn:JCouch.Project project.xsd"
       elementFormDefault="qualified"
    >

  <!-- import all other related schemas here -->
  <xs:import namespace="urn:JCouch.Project" schemaLocation="project.xsd"/>
  <xs:import namespace="urn:JCouch.Attachment" schemaLocation="attachment.xsd"/>

  <!-- all other definitions of Document.xsd -->
  ...

</xs:schema>

现在,Document.xsd 应该验证您的 XML.

Now, Document.xsd should validate your XML.

(实际上,您可以通过在其中指定所有其他模式的类似导入来从任何其他模式创建驱动程序.)

(In fact, you can make a driver from any other schema by specifying in it similar imports of all other schemas.)

这篇关于使用多个 xsd 文件验证 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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