【问题标题】:Validate xml with multiple xsd files使用多个 xsd 文件验证 xml
【发布时间】:2013-09-03 11:29:24
【问题描述】:

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

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

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

我想要以下 xml 布局:

 <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 等等。

文档.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:

<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>

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

<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>

任何帮助将不胜感激。

最好的问候 克雷斯滕·克亚尔

【问题讨论】:

    标签: xml xsd xsd-validation


    【解决方案1】:

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

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

    <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。

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

    【讨论】:

    • 首先。谢谢!这解决了我的问题。我希望我的命名空间会被 xxi:schemaLocation 属性引入。
    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2012-09-20
    • 2010-09-26
    • 2012-06-04
    相关资源
    最近更新 更多