我正在实现符合两个外部提供的XSD的XML解决方案。首先,我们有ns1.xsd:
<schema xmlns:ns1="http://www.test.com/ns1"
xmlns:ns2="http://www.test.com/ns2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema.xsd"
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.test.com/ns1"
elementFormDefault="qualified">
<import namespace="http://www.test.com/ns2"
schemaLocation="http://www.test.com/ns2.xsd"/>
<element name="Root">
<complexType>
<sequence>
<element name="Child" type="ns2:ChildType"
minOccurs="0"/>
</sequence>
</complexType>
<attribute ref="ns2:field3" use="optional"/>
</element>
</schema>
和ns2.xsd:
<schema xmlns:ns2="http://www.test.com/ns2" xmlns:xsd="http://www.w3.org/2001/XMLSchema.xsd" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.test.com/ns2" elementFormDefault="qualified" attributeFormDefault="qualified">
<complexType name="ChildType">
<attribute name="field1" type="string" use="optional"/>
<attribute name="field2" type="string" use="optional"/>
</complexType>
<attribute name="field3" type="string"/>
</schema>
在liquid-technologies.com结束时,应该实施tutorial showing how XSDs using referenced types。按照这个逻辑,我得到:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns="http://www.test.com/ns1"
xmlns:ns2="http://www.test.com/ns2"
ns2:field3="test">
<Child ns2:field1="test" ns2:field2="test"/>
</Root>
使用Xerces 2.11.0进行验证。如果我在ns2.xsd中更改attributeFormDefault="unqualified"
,我必须删除实现中的名称空间前缀以使其验证:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns="http://www.test.com/ns1"
xmlns:ns2="http://www.test.com/ns2"
ns2:field3="test">
<Child field1="test" field2="test"/>
</Root>
当我asked a while back时,field3
(正确)仍然是前缀。现在我想知道:
field1
相比,field2
和field3
之间的真正区别是什么?为什么Xceres强迫我省略field1
和field2
的前缀?是因为field1
和field2
确实是某种类型的一部分,而field3
是对属性的引用吗?field1
和field2
所属的命名空间属性?(如果有人知道描述这些规则的W3C recs的哪一部分,我也会非常感激。)
其他信息
我觉得有必要指出这个例子的后果。如果1 field1
和field2
以ns2
为前缀,则会在ns2
- 命名空间中明确将其设置为属性名称。在情况2中,所述属性名称都没有前缀,除了这些属性属于默认(ns1
)命名空间之外,很难得出任何其他结论。
为什么这很重要?嗯,这很重要,因为它有效地将attributeFormDefault
转换为命名空间限定符。我很难理解这是W3C委员会的意图,因此我认为这是一个错误。如果有人能够启发我,我会很激动!
答案 0 :(得分:1)
没有前缀的属性通常被视为没有命名空间。它们不在默认命名空间中。 (我一般说,因为有些人喜欢不同的解释,比如他们在一个未指定的名称空间中,但这种区别对我来说太微妙了。)
当你说attributeFormDefault =“unqualified”时,ns2架构文档中的本地声明的属性将不在命名空间中,这意味着它们必须出现在没有前缀的实例中。
至于ns2:field3,这有点奇怪,因为似乎没有该属性的声明。但是如果有的话,那就必须是全局属性声明,全局属性总是放在包含模式文档的目标命名空间中。