How to reference element in other XSD's namesp

2020-04-18 09:03发布

问题:

I have a complexType that's defined across two .XSD files.

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 name="embedded" type="emb:EmbeddedType"></xs:element>
            </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="urn:Embedded" targetNamespace="urn:Embedded" 
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

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

And my XML looks something like this:

<?xml version="1.0" encoding="utf-8"?>

    <ParentType xmlns="urn:Parent" xmlns:emb="urn:Embedded">
        <embedded>
            <emb:numeric>12</emb:numeric>
            <emb:embedded>
                <emb:numeric>5</emb:numeric>
            </emb:embedded>
        </embedded>
    </ParentType>

Is there a way to alter things so that both embedded elements have the same prefix? I can't just copy the EmbeddedType into Parent.xsd for external reasons. Further, a solution that eliminates all usage of the emb prefix indiscriminately will not work.

回答1:

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>