【问题标题】:How XSD refers to XML elements with same name in different namespacesXSD 如何引用不同命名空间中同名的 XML 元素
【发布时间】:2015-11-11 19:49:17
【问题描述】:

我有这个 XML 模式,它包含一个序列,其中两个 ref 名称相同,但命名空间不同。 Address 元素在 Address1.xsdAddress2.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>

【讨论】:

  • 谢谢大家。 @kjhughes
猜你喜欢
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多