对于 XML 模式中的 targetNamespace
属性如何影响元素的命名,我有些困惑。我在验证以下内容时遇到错误:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" version="1.0">
<xs:element name="testType" type="testType"/>
<xs:complexType name="testType">
<xs:sequence>
<xs:element name="testSubtype" type="testSubType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="testSubType">
<!-- some fields -->
</xs:complexType>
</xs:schema>
XMLSpy 声明 无法解析不合格的声明或定义“testSubType”。
我该如何解决这个问题?我需要在那里保留 targetNamespace
属性。我尝试在各个领域将 testSubType
更改为 test:testSubType
但这似乎不起作用。
最佳答案
要么将 xmlns="test"
属性添加到架构元素以声明此架构的默认命名空间为“test”,要么添加 xmlns:t="test"
来声明 t
是“test”命名空间的前缀,并在引用您在此命名空间中定义的类型时使用该前缀,如 type=t:testSubType
你通过说 test 是你的 targetNamespace 来做)。
关于XML targetNamespace 和元素的非限定声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7743953/