X包括架构/命名空间验证?
我正在尝试使用 XML Includes 来帮助管理需要可供人类和机器使用的大型 XML 结构。
但是,在尝试构建可根据现有架构进行验证的 XML 文件时,我遇到了无数问题。 这是我正在尝试做的事情的一个简化示例。
我的“main.xml”文件未验证。
<?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”文件作为独立文件可以很好地验证。
<?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>
我的问题几乎显然与名称空间相关,但我不知道如何解决我的问题。
I'm trying to use XML Includes to help manage a large XML structure that needs to be usable by both humans and machines.
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.
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>
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>
Here's my schema:
<?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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 skaffman 已经指出的那样,XML Schema 和 XInclude 不兼容。
来自 xmllint 的验证错误消息清楚地说明了这一点:
引用 W3C 建议:“XInclude 定义与应用 XML 模式生成的增强信息集没有任何关系。可以提供这样的增强信息集。作为输入信息集,或者这种增强可能会应用于包含在内所产生的信息集。”
因此,您应该首先通过应用 XInclude 构建整个 XML 文件,然后验证该文件。
编辑:您可以使用 xmllint 和 --xinclude 来验证 main.xml。
As skaffman already pointed out, XML Schema and XInclude are not compatible.
The validation error message from xmllint states this clearly:
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."
Thus you should first construct the whole XML file by applying the XIncludes, and validate this file afterwards.
Edit: You can use xmllint with --xinclude to validate main.xml.
我同意 grantwparks - XInclude 和 XML Schema 绝对可以一起使用。 这些规范有意相互独立。 显然,XInclude 的作者希望提供在验证之前或验证之后处理包含的自由。
这个velocityreviews 上的帖子讨论了对我有帮助的问题和答案要了解此事,请参阅 xml.com 上的这篇文章,引用其中:
所以它似乎可以归结为,让您的 XML 工具在验证之前处理 xi:include 元素(这就是 OP 示例所需要的)。 例如,在Eclipse的XML编辑器中,XML->XML下有一个设置“Process XML Inclusions”。 XML 文件 -> 验证(使用 RSA 8.5),需要打开它才能让编辑器在验证之前处理 xi:include。
安迪
I agree with grantwparks - XInclude and XML Schema absolutely can be used together. The specifications are intentionally independent from each other. Apparently the authors of XInclude wanted to provide the freedom to let the includes be processed before validation or after validation.
This thread on velocityreviews discusses the issue and the answer that helped me to understand the matter was this post on xml.com, quoting from it:
So what it seems to boil down to, is to get your XML tools to process the xi:include elements before validation (that is what the example of the OP needs). For example, in the XML Editor of Eclipse, there is a setting "Process XML Inclusions" under XML -> XML Files -> Validation (using RSA 8.5), which needs to be turned on to get the editor to process xi:include before validation.
Andy
我不认为这是由 XInclude 和 schema 之间不兼容引起的; 在我看来,包含在验证之前没有被处理。 因此,该架构不允许 main 中存在“include”元素,而仅允许“child”元素。 如果您可以强制 XML 处理器在验证之前处理包含内容...
I don't think this is caused by an incompatibility between XInclude and schema; it looks to me like the include is not being processed before validation. Thus the schema does not allow for an "include" element in main, only a "child" element. If you could force your XML processor to process includes before validation...
我不认为 XML Schema 和 XInclude 规范保证彼此兼容。 个别 XML 处理器可能允许这样做,但其他处理器则不允许。
作为一般规则,我认为两者不应该一起使用。
PS 我不确定你为什么认为这是一个命名空间问题。 是什么给你这样的印象?
I don't think the XML Schema and XInclude specifications are guaranteed to be compatible with each other. Individual XML processors might allow it, but others will not.
As a general rule, I'd say that the two should not be used together.
P.S. I'm not sure why you think this is a namespace problem. What gives you that impression?