参考 XSD 模型组的特定元素? [英] Refer to specific element of an XSD model group?

查看:28
本文介绍了参考 XSD 模型组的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以创建一个组并引用该组的元素吗?

Can we create a group and refer to element of that group?

例如,我们有一个小组说

For example, we have a group say

<xs:group name="custGroup">
  <xs:sequence>
    <xs:element name="customerId" type="xs:string"/>
    <xs:element name="customerName" type="xs:string"/>
    <xs:element name="Address1" type="xs:string"/>
    <xs:element name="Address2" type="xs:string"/>
    <xs:element name="mobile" type="xs:string"/>
  </xs:sequence>
</xs:group>

假设我想创建另一个只有 customerIdmobile 的元素:

Suppose I want to create another element to have only customerId and mobile:

<xs:element name="custBrief">
  <xs:sequence>
    <xs:element name="customerId" type="xs:string"/>
    <xs:element name="mobile" type="xs:string"/>
  </xs:sequence>
</xs:element>

所以,我应该可以参考custGroup.

So, I should be able to refer to custGroup.

推荐答案

选项 1:通过引用而不是名称来声明组成元素来重用组的部分:

Option 1: Re-use parts of your group by declaring the constituent elements by reference rather than by name:

<xs:group name="custGroup">
  <xs:sequence>
    <xs:element ref="customerId"/>
    <xs:element ref="customerName"/>
    <xs:element ref="Address1"/>
    <xs:element ref="Address2"/>
    <xs:element ref="mobile"/>
  </xs:sequence>
</xs:group>

<xs:element name="customerId" type="xs:string"/>
<xs:element name="customerName" type="xs:string"/>
<xs:element name="Address1" type="xs:string"/>
<xs:element name="Address2" type="xs:string"/>
<xs:element name="mobile" type="xs:string"/>

这样,您的第二个类似的组至少可以共享引用元素的全局声明:

This way, your second, similar group can at least share the global declarations of the referenced elements:

<xs:element name="custBrief">
  <xs:sequence>
    <xs:element ref="customerId"/>
    <xs:element ref="mobile"/>
  </xs:sequence>
</xs:element>

选项 2:使用子组:

<xs:group name="custGroup">
  <xs:sequence>
    <xs:group ref="custBriefGroup"/>
    <xs:element ref="customerName"/>
    <xs:element ref="Address1"/>
    <xs:element ref="Address2"/>
  </xs:sequence>
</xs:group>

<xs:group name="custBriefGroup">
  <xs:sequence>
    <xs:element name="customerId" type="xs:string"/>
    <xs:element name="mobile" type="xs:string"/>
  </xs:sequence>
</xs:group>

<xs:element name="custBrief">
  <xs:sequence>
    <xs:group ref="custBriefGroup"/>
  </xs:sequence>
</xs:element>

这样,custBriefGroup 就在一个地方定义并重复使用.

This way, custBriefGroup is defined in one place an re-used.

请注意,我在重新安排元素顺序方面很随意.

Note that I took liberties with re-arranging element ordering.

另见:之间的区别;<序列><选择>和<组>在 XSD 中?

这篇关于参考 XSD 模型组的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆