XSD 使用 targetNamespace 分离复杂类型
XSD with separated complex types using targetNamespace
我写的很简单XML:
<?xml version="1.0" encoding="utf-8"?>
<something attribute1="21" attribute2="23">
<newelement code="code1"/>
</something>
我想写一个 XSD 来验证这个 XML,效果很好:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="something">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="newelement" nillable="true">
<xs:complexType>
<xs:attribute type="xs:string" name="code"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attribute1" type="xs:int"/>
<xs:attribute name="attribute2" type="xs:int"/>
</xs:complexType>
</xs:element>
</xs:schema>
但后来我想写相同的 XSD,但使用 分隔的复杂类型 ,因为例如,如果我需要与 newelement
现在有了。所以我用这种方式重构了我的 XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="my-common-types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="my-common-types">
<xs:element name="something">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="newelement" nillable="true" type="tns:ElementWithCode"/>
</xs:sequence>
<xs:attribute name="attribute1" type="xs:int"/>
<xs:attribute name="attribute2" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:complexType name="ElementWithCode">
<xs:attribute name="code" type="xs:string"/>
</xs:complexType>
</xs:schema>
然后,我总是得到这个错误:
ERROR: Element 'something': No matching global declaration available
for the validation root.
因此在方案中使用 targetNamespace
属性时出现问题,但我不明白如何才能使它正常工作。请给我一些建议。谢谢!
您的重构架构适用于命名空间 "my-common-types",而原始架构没有命名空间。如果您希望您的元素不在命名空间中,那么(全局)元素声明必须在没有 targetNamespace
的模式文档中。如果需要,您仍然可以将类型声明放在命名空间中,但它们必须位于单独的模式文档中,该文档使用 xs:import
.
导入到无命名空间模式文档中
XML 和 XSD 都缺少部分。
XML 缺少 <something>
元素中的 xmlns="my-common-types"
属性。
XSD 缺少 <xs:schema>
元素中的 elementFormDefault="qualified"
属性。
我写的很简单XML:
<?xml version="1.0" encoding="utf-8"?>
<something attribute1="21" attribute2="23">
<newelement code="code1"/>
</something>
我想写一个 XSD 来验证这个 XML,效果很好:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="something">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="newelement" nillable="true">
<xs:complexType>
<xs:attribute type="xs:string" name="code"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attribute1" type="xs:int"/>
<xs:attribute name="attribute2" type="xs:int"/>
</xs:complexType>
</xs:element>
</xs:schema>
但后来我想写相同的 XSD,但使用 分隔的复杂类型 ,因为例如,如果我需要与 newelement
现在有了。所以我用这种方式重构了我的 XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="my-common-types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="my-common-types">
<xs:element name="something">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="newelement" nillable="true" type="tns:ElementWithCode"/>
</xs:sequence>
<xs:attribute name="attribute1" type="xs:int"/>
<xs:attribute name="attribute2" type="xs:int"/>
</xs:complexType>
</xs:element>
<xs:complexType name="ElementWithCode">
<xs:attribute name="code" type="xs:string"/>
</xs:complexType>
</xs:schema>
然后,我总是得到这个错误:
ERROR: Element 'something': No matching global declaration available for the validation root.
因此在方案中使用 targetNamespace
属性时出现问题,但我不明白如何才能使它正常工作。请给我一些建议。谢谢!
您的重构架构适用于命名空间 "my-common-types",而原始架构没有命名空间。如果您希望您的元素不在命名空间中,那么(全局)元素声明必须在没有 targetNamespace
的模式文档中。如果需要,您仍然可以将类型声明放在命名空间中,但它们必须位于单独的模式文档中,该文档使用 xs:import
.
XML 和 XSD 都缺少部分。
XML 缺少 <something>
元素中的 xmlns="my-common-types"
属性。
XSD 缺少 <xs:schema>
元素中的 elementFormDefault="qualified"
属性。