我正在为Couchdb创建一个xml链接,并使用xsd验证文档。
现在项目的嵌入:项目不起作用,并给出了验证错误。
如何更改我的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个用于项目。 记住项目只是一个例子。我后来想添加博客:博客博客:条目等等。
Document.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>
最后是attachment.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>
非常感谢任何帮助。
祝你好运 Kresten Kjaer
答案 0 :(得分:2)
由于所有三个模式都有不同的目标名称空间, 您可以使用其中一个作为主模式(或驱动程序模式), 您将指定给验证器, 并且必须在其中导入所有其他模式。
让我们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。
(实际上,您可以通过在其中指定所有其他模式的类似导入来从任何其他模式创建驱动程序。)