2


I have an element defined in a common schema that will be used by several other schemas. How do I refer to said element in the schemas that import the common one?
Let me give some more details... here is the common schema:

<xs:schema 
    xmlns="http://schemas.com/ns/2013/06/research/monitoring" 
    targetNamespace="http://schemas.com/ns/2013/06/research/monitoring" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
>
<xs:complexType name="SQLSettings">
  <xs:sequence>
    <xs:element name="MasterSqlServerName" type="xs:string"/>
    <xs:element name="MasterSqlServerNameRO" type="xs:string"/>
    <xs:element name="MasterDatabaseName" type="xs:string"/>
    <xs:element name="UserID" type="xs:string"/>
    <xs:element name="Password" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="heartbeatResp">
  <xs:sequence>     
    <xs:element name="SQLSettings" type="SQLSettings"/>         
    <xs:element name="ComputerName" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

<xs:element name="heartbeatResp" type="heartbeatResp"/>

</xs:schema>

... and this would be a schema that imports the common one:

<xs:schema xmlns="http://schemas.com/ns/2005/10/userinfoupdate" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://schemas.com/ns/2005/10/userinfoupdate" elementFormDefault="qualified" 
xmlns:mon="http://schemas.com/ns/2013/06/research/monitoring"
>

<xs:import namespace="http://schemas.com/ns/2013/06/research/monitoring" schemaLocation="common/schemas/research_monitoring.xsd" />

... declare here the heartbeatResp element of the common schema ...

</xs:schema>

Is there any way to do this? I have tried to make <xs:element ref="mon:heartbeatResp"/>, but then I discovered that global element declarations cannot contain references.
I know I could also move the element declaration to my specific schema and only refer to the complexType in the common one - like <xs:element name="heartbeatResp" type="mon:heartbeatResp"/>... but this leads to other problems, and it is more of a workaround...
What I really want know is: can I import a global element from a schema into another?

4

1 回答 1

3

The import is enough to import the element. Then it just depends what you want to do with it. If you want to make it part of the content model of a complex type, then include an element particle that refers to it, for example <element ref="mon:heartBeatresp"/>, or if you want to add an element to its substitution group, use substitutionGroup="mon:heartBeatresp".

于 2013-06-17T14:58:40.130 回答