How to reference element in other XSD's namespace?

為{幸葍}努か 提交于 2019-12-02 05:59:31

If you want the first embedded to be in the urn:Embedded namespace along with the second embedded, you can move it there via xs:element/@ref:

Parent.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
           xmlns:emb="urn:Embedded"
           targetNamespace="urn:Parent"
           elementFormDefault="qualified"              
           attributeFormDefault="unqualified">

  <xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>

  <xs:element name="ParentType">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="emb:embedded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Embedded.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:emb="urn:Embedded"
           targetNamespace="urn:Embedded" 
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

  <xs:element name="embedded" type="emb:EmbeddedType"/>

  <xs:complexType name="EmbeddedType">
    <xs:sequence>
      <xs:element name="numeric" type="xs:int"/>
      <xs:element name="embedded" type="emb:EmbeddedType" 
                  minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

XML

<?xml version="1.0" encoding="utf-8"?>
<ParentType xmlns="urn:Parent" 
            xmlns:emb="urn:Embedded"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:Parent Parent.xsd">
  <emb:embedded>
    <emb:numeric>12</emb:numeric>
    <emb:embedded>
      <emb:numeric>5</emb:numeric>
    </emb:embedded>
  </emb:embedded>
</ParentType>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!