简体   繁体   English

XML模式:如何从另一个模式导入全局元素?

[英]XML schema: how to import a global element from another schema?


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. 我试图使<xs:element ref="mon:heartbeatResp"/> ,但是后来我发现全局元素声明不能包含引用。
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... 我知道我也可以将元素声明移到我的特定模式,并且仅在通用模式中引用complexType,例如<xs:element name="heartbeatResp" type="mon:heartbeatResp"/> ...但这会导致其他问题,这更像是一种解决方法...
What I really want know is: can I import a global element from a schema into another? 我真正想知道的是:我可以将全局元素从一个模式导入另一个模式吗?

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" . 如果要使其成为复杂类型的内容模型的一部分,则包括引用该元素的元素粒子,例如<element ref="mon:heartBeatresp"/> ,或者要向其添加元素替换组,请使用substitutionGroup="mon:heartBeatresp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM