参考 XSD 模型组的特定元素? [英] Refer to specific element of an XSD model group?
问题描述
我们可以创建一个组并引用该组的元素吗?
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>
假设我想创建另一个只有 customerId
和 mobile
的元素:
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 模型组的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!