gpt4 book ai didi

xml - 一个 xml 命名空间等于一个且只有一个模式文件?

转载 作者:数据小太阳 更新时间:2023-10-29 01:38:37 26 4
gpt4 key购买 nike

...或者为什么这些文件在 Visual Studio 2010 中验证而不是 xmllint1

我目前正在处理一个已发布的 xml 架构,其中原作者的习惯是将架构分解为多个 .xsd 文件,但其中一些架构文件具有相同的 targetNamespace。这真的是“允许的”吗?

示例(极度简化):

File    targetNamespace    Contents
------------------------------------------------------------
b1.xsd uri:tempuri.org:b complex type "fooType"
b2.xsd uri:tempuri.org:b simple type "barType"

a.xsd uri:tempuri.org:a imports b1.xsd and b2.xsd
definition of root element "foo", that
extends "b:fooType" with an attribute
of "b:barType"

(下面是完整的文件内容。)

然后我有一个 xml 文件,data.xml,内容如下:

<?xml version="1.0"?>
<foo bar="1" xmlns="uri:tempuri.org:a" xmlns:xs="http://www.w3.org/2001/XMLSchema" />

很长一段时间以来,我一直相信所有这些都是正确的,因为 Visual Studio 显然允许这种架构样式。然而,今天我决定设置一个命令行实用程序来验证 xml 文件,我选择了 xmllint

当我运行 xmllint --schema a.xsd data.xml 时,我看到了这个警告:

a.xsd:4: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Skipping import of schema located at 'b2.xsd' for the namespace 'uri:tempuri.org:b', since this namespace was already imported with the schema located at 'b1.xsd'.

b2.xsd 的导入被跳过的事实显然导致了这个错误:

a.xsd:9: element attribute: Schemas parser error : attribute decl. 'bar', attribute 'type': The QName value '{uri:tempuri.org:b}barType' does not resolve to a(n) simple type definition.

如果 xmllint 是正确的,那么我正在处理的已发布规范中就会有错误。 有吗? Visual Studio 会出错。 是吗?

我确实意识到 xs:importxs:include 之间的区别。现在,我只是不明白 xs:include 如何解决问题,因为:

  • b1.xsdb2.xsd 有相同的targetNamespace
  • 它们在 targetNamespacea.xsd 上都不同
  • 并且他们不(不需要)了解对方

这是原始模式规范中的缺陷吗?我开始认为第三点至关重要。他们彼此不了解这一事实是否应该导致将他们放在不同的命名空间中?


b1.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="uri:tempuri.org:b" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="fooType" />
</xs:schema>

b2.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="uri:tempuri.org:b" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="barType">
<xs:restriction base="xs:integer" />
</xs:simpleType>
</xs:schema>

a.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="uri:tempuri.org:a" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:b="uri:tempuri.org:b">
<xs:import namespace="uri:tempuri.org:b" schemaLocation="b1.xsd" />
<xs:import namespace="uri:tempuri.org:b" schemaLocation="b2.xsd" />
<xs:element name="foo">
<xs:complexType>
<xs:complexContent>
<xs:extension base="b:fooType">
<xs:attribute name="bar" type="b:barType" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>

注释:

1) 我正在使用 libxml2/xmllint 的 Windows 端口在 www.zlatkovic.com 找到.

最佳答案

这里问题的症结在于,当您有两个不同的 <import> 时,这意味着什么?元素,当它们都引用相同的命名空间时。

当您考虑 schemaLocation 时,它有助于阐明含义<import> 的属性完全是可选的。当您省略它时,您只是在说“我想将命名空间 XYZ 的模式导入到这个 模式中”。 schemaLocation只是关于在哪里可以找到其他模式的定义的提示。

<import>的准确含义当您阅读 W3C 规范时有点模糊,可能是故意的。结果,解释各不相同。

一些 XML 处理器容忍多个 <import>对于相同的命名空间,并基本上合并所有 schemaLocation成一个单一的目标。

其他处理器更严格,决定只有一个<import>每个目标 namespace 均有效。我认为这是更正确的,当你考虑到 schemaLocation是可选的。

除了你给的VS和xmllint的例子,Xerces-J也是超严格的,忽略后面的<import>对于相同的目标 namespace ,给出与 xmllint 相同的错误。另一方面,XML Spy 更为宽松(但是 XML Spy 的验证是出了名的不稳定)

为了安全起见,您应该有这些多重导入。一个给定的命名空间应该有一个单一的“主”文档,它又有一个 <include>对于每个子文档。这个主人通常是高度虚假的,只充当一个容器。对于这些子文档。

据我所知,这通常包含 XML 模式在最大工具兼容性方面的“最佳实践”,但有些人会争辩说这是一种摆脱优雅模式设计的技巧。

嗯。

关于xml - 一个 xml 命名空间等于一个且只有一个模式文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4998063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com