【问题标题】:How XSD refers to XML elements with same name in different namespacesXSD 如何引用不同命名空间中同名的 XML 元素
【发布时间】:2015-11-11 19:49:17
【问题描述】:
我有这个 XML 模式,它包含一个序列,其中两个 ref 名称相同,但命名空间不同。
Address 元素在 Address1.xsd 和 Address2.xsd
中都有定义
我想知道这是否被标准接受。
<xsd:schema targetNamespace="http://xmlns.oracle.com/bpmn/bpmnCloudProcess/Testnamespace/Process"
xmlns:tns7="http://my.namespace.com2" xmlns:tns6="http://my.namespace.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://my.namespace.com" schemaLocation="Address1.xsd"/>
<xsd:import namespace="http://my.namespace.com2" schemaLocation="Address2.xsd"/>
<xsd:element name="start">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="tns6:Address"/>
<xsd:element ref="tns7:Address"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
【问题讨论】:
标签:
xml
reference
xsd
namespaces
xml-namespaces
【解决方案1】:
是的,您的示例是正确的,并演示了引用两个 Address 元素的正确方法,这些元素在不同的命名空间中进行了区分。只要确保 Address1.xsd 的 targetNamespace 等于 http://my.namespace.com 并且 Address2.xsd 的 targetNamespace 等于 http://my.namespace.com2。以下是所有三个一致定义的 XSD:
主要 XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
targetNamespace="http://xmlns.oracle.com/bpmn/bpmnCloudProcess/Testnamespace/Process"
xmlns:tns7="http://my.namespace.com2"
xmlns:tns6="http://my.namespace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://my.namespace.com"
schemaLocation="Address1.xsd"/>
<xsd:import namespace="http://my.namespace.com2"
schemaLocation="Address2.xsd"/>
<xsd:element name="start">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="tns6:Address"/>
<xsd:element ref="tns7:Address"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
地址1.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://my.namespace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Address"/>
</xsd:schema>
地址2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://my.namespace.com2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Address"/>
</xsd:schema>