Refer to specific element of an XSD model group?

六眼飞鱼酱① 提交于 2019-12-24 07:33:14

问题


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>

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>

So, I should be able to refer to custGroup.


回答1:


You're mixing two different schema construction models here.

You can assemble a type from a set of building blocks by using model groups.

You can "subset" a type by using derivation-by-restriction.

But you can't mix the two in the way you are attempting.




回答2:


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>

Option 2: Use subgroups:

<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>

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

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

See also: The difference between <all> <sequence> <choice> and <group> in XSD?



来源:https://stackoverflow.com/questions/47017931/refer-to-specific-element-of-an-xsd-model-group

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