限制来自其他命名空间的 XML 类型
Restrict XML type from other namespace
我想从基础 XML 文件中定义的类型继承,但我想做一个修改。在我的新模式文件中,基本文件模式中可选的元素必须是必需的。名称空间不同,我无法更改它们。什么是更好的解决方案?我有下面的代码,但出现错误。
文件base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://basetype"
xmlns="http://basetype">
<xs:element name="BaseTypeElement" type="BaseType"/>
<xs:complexType name="BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
文件restrictbasetype.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://restrictbasetype"
xmlns:bas="http://basetype"
xmlns="http://restrictbasetype">
<xs:import schemaLocation="base.xsd" namespace="http://basetype"/>
<xs:element name="RestrictedElement" type="BaseTypeRestriction"/>
<xs:complexType name="BaseTypeRestriction">
<xs:complexContent>
<xs:restriction base="bas:BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<!-- name is now mandatory -->
<xs:element name="name" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
错误是因为名称空间之间的冲突。
您的第一个模式 base.xsd
具有 {http://basetype}
目标命名空间。
您的第二个模式 restrictbasetype.xsd
具有 {restrictbasetype.xsd}
目标命名空间。此外,您的模式都在其头部指定 elementFormDefault="qualified"
属性,这意味着所有本地声明的元素都是合格的,即属于其模式的目标名称空间。对于 XML 处理器,这意味着您的两个模式都定义了完全不同的事物。所以,他们可能只是共存,但不能相互强制执行任何事情。
具体来说,这是错误的地方:
<xs:complexType name="BaseTypeRestriction">
<xs:complexContent>
<!-- The restriction means that anything defined inside it may be just
overridings fully compatible with the content defined by parent type: 'bas:BaseType'
-->
<xs:restriction base="bas:BaseType">
<xs:sequence>
<!-- But here you actually declare an element with the qualified name:
{http://restrictbasetype}:id. It won't override the 'id' element in
'bas:BaseType' because that one has the qulified name: {http://basetype}:id.
On the other hand, you cannot even extend the content of 'bas:BaseType'
because it is the restriction.
So, this particular declaration is completely wrong!
-->
<xs:element name="id" type="xs:long"/>
<!-- the same is for this -->
<xs:element name="name" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
那么,您可以做些什么来实现您的意图呢?您应该 remove elementFormDefault="qualified"
在您的两个模式头中。然后,任何本地声明的元素都将在相同的 {no namespace}
中,因此可以覆盖。
这些模式对我有用:
base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- no 'elementFormDefault' attribute -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://basetype"
xmlns="http://basetype">
<xs:element name="BaseTypeElement" type="BaseType"/>
<xs:complexType name="BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
restrictbasetype.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- no 'elementFormDefault' attribute -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://restrictbasetype"
xmlns:bas="http://basetype"
xmlns="http://restrictbasetype">
<xs:import schemaLocation="base.xsd" namespace="http://basetype"/>
<xs:element name="RestrictedElement" type="BaseTypeRestriction"/>
<xs:complexType name="BaseTypeRestriction">
<xs:complexContent>
<xs:restriction base="bas:BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<!-- name is now mandatory -->
<xs:element name="name" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
促销插件。对于使用 XML 模式和 WSDL 的人来说,这些链接可能会很有趣:FlexDoc/XML XSDDoc & WSDLDoc – 高性能通用 XML 模式/带图的 WSDL 文档生成器
我想从基础 XML 文件中定义的类型继承,但我想做一个修改。在我的新模式文件中,基本文件模式中可选的元素必须是必需的。名称空间不同,我无法更改它们。什么是更好的解决方案?我有下面的代码,但出现错误。
文件base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://basetype"
xmlns="http://basetype">
<xs:element name="BaseTypeElement" type="BaseType"/>
<xs:complexType name="BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
文件restrictbasetype.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://restrictbasetype"
xmlns:bas="http://basetype"
xmlns="http://restrictbasetype">
<xs:import schemaLocation="base.xsd" namespace="http://basetype"/>
<xs:element name="RestrictedElement" type="BaseTypeRestriction"/>
<xs:complexType name="BaseTypeRestriction">
<xs:complexContent>
<xs:restriction base="bas:BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<!-- name is now mandatory -->
<xs:element name="name" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
错误是因为名称空间之间的冲突。
您的第一个模式 base.xsd
具有 {http://basetype}
目标命名空间。
您的第二个模式 restrictbasetype.xsd
具有 {restrictbasetype.xsd}
目标命名空间。此外,您的模式都在其头部指定 elementFormDefault="qualified"
属性,这意味着所有本地声明的元素都是合格的,即属于其模式的目标名称空间。对于 XML 处理器,这意味着您的两个模式都定义了完全不同的事物。所以,他们可能只是共存,但不能相互强制执行任何事情。
具体来说,这是错误的地方:
<xs:complexType name="BaseTypeRestriction">
<xs:complexContent>
<!-- The restriction means that anything defined inside it may be just
overridings fully compatible with the content defined by parent type: 'bas:BaseType'
-->
<xs:restriction base="bas:BaseType">
<xs:sequence>
<!-- But here you actually declare an element with the qualified name:
{http://restrictbasetype}:id. It won't override the 'id' element in
'bas:BaseType' because that one has the qulified name: {http://basetype}:id.
On the other hand, you cannot even extend the content of 'bas:BaseType'
because it is the restriction.
So, this particular declaration is completely wrong!
-->
<xs:element name="id" type="xs:long"/>
<!-- the same is for this -->
<xs:element name="name" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
那么,您可以做些什么来实现您的意图呢?您应该 remove elementFormDefault="qualified"
在您的两个模式头中。然后,任何本地声明的元素都将在相同的 {no namespace}
中,因此可以覆盖。
这些模式对我有用:
base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- no 'elementFormDefault' attribute -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://basetype"
xmlns="http://basetype">
<xs:element name="BaseTypeElement" type="BaseType"/>
<xs:complexType name="BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
restrictbasetype.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- no 'elementFormDefault' attribute -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://restrictbasetype"
xmlns:bas="http://basetype"
xmlns="http://restrictbasetype">
<xs:import schemaLocation="base.xsd" namespace="http://basetype"/>
<xs:element name="RestrictedElement" type="BaseTypeRestriction"/>
<xs:complexType name="BaseTypeRestriction">
<xs:complexContent>
<xs:restriction base="bas:BaseType">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<!-- name is now mandatory -->
<xs:element name="name" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
促销插件。对于使用 XML 模式和 WSDL 的人来说,这些链接可能会很有趣:FlexDoc/XML XSDDoc & WSDLDoc – 高性能通用 XML 模式/带图的 WSDL 文档生成器