XSD with elements from other namespace

半城伤御伤魂 提交于 2019-11-27 19:06:40
daveaglick

There are actually two different ways to compose XML Schema documents: <xs:import> and <xs:include>. xs:include is intended to be used when the namespace of the containing document is the same as the one being referenced, so it's not quite what you're looking for. xs:import is better for your situation when you need to reference all (or a subset) of elements in the referenced schema and they're in a different target namespace. There's a question here on the differences: What's the difference between xsd:include and xsd:import?.

Anyway, back to this specific question. What you probably want is something like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema 
    xmlns="http://example.com/namespace/for/ElementB"
    targetNamespace="http://example.com/namespace/for/ElementB"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    xmlns:ea="http://example.com/namespace/for/ElementA">
 <xs:import namespace="http://example.com/namespace/for/ElementA" schemaLocation="A.xsd" /> 
 <xs:element name="ElementB">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="foo" type="xs:string" />
    <xs:element name="bar" type="xs:string" />
    <!-- This introduces a element named ElementA that uses the ComplexType ea:ElementA defined in A.xsd -->
    <xs:element name="ElementA" type="ea:ElementA" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

Though you'll need A.xsd to create a complex type for ElementA that you can use in B.xsd as shown.

This arcticle has some good information/examples and includes a discussion of some of the different composability strategies: http://www.xfront.com/ZeroOneOrManyNamespaces.html

Alexander Yezutov

You could use <xsd:import> tag to import a schema with another namespace.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!