XQuery:提取所有嵌套 xs:attributeGroup

XQuery: extract all nested xs:attributeGroup

<xs:attributeGroup name="personattr">
  <xs:attribute name="attr1" type="xs:string"/>
  <xs:attribute name="attr2" type="xs:integer"/>
  <xs:attributeGroup ref="personattr2">
</xs:attributeGroup>

<xs:attributeGroup name="personattr2">
  <xs:attribute name="attr12" type="xs:string"/>
  <xs:attribute name="attr22" type="xs:integer"/>
  <xs:attributeGroup ref="personattr3">
</xs:attributeGroup>

<xs:attributeGroup name="personattr3">
  <xs:attribute name="attr13" type="xs:string"/>
  <xs:attribute name="attr23" type="xs:integer"/>
</xs:attributeGroup>

<xs:attributeGroup name="personattr4">
  <xs:attribute name="attr14" type="xs:string"/>
  <xs:attribute name="attr24" type="xs:integer"/>
</xs:attributeGroup>

鉴于上面的示例 XSD 文件,我如何提取从特定 attributeGroup 开始的所有嵌套 attributeGroup?从personattr里面有个personattr2。在 personattr2 里面有 personattr3。结果不应包含 personattr4.

您可以通过使用递归 用户定义函数:

下降到引用的子组来轻松做到这一点
declare function local:groups-below($groups, $name) {
  $name,
  for $ref in $groups[@name = $name]/xs:attributeGroup/@ref/string()
  return local:groups-below($groups, $ref)
};

local:groups-below($all-groups, 'personattr')

这 return 组名称如下:

personattr
personattr2
personattr3

如果您想要整个元素,只需 return 那些而不是它们的名称:

declare function local:groups-below($groups, $name) {
  let $group := $groups[@name = $name]
  return (
    $group,
    for $ref in $group/xs:attributeGroup/@ref/string()
    return local:groups-below($groups, $ref)
  )
};

local:groups-below($all-groups, 'personattr')

这 return 是以下序列:

<xs:attributeGroup xmlns:xs="http://www.w3.org/2001/XMLSchema" name="personattr">
  <xs:attribute name="attr1" type="xs:string"/>
  <xs:attribute name="attr2" type="xs:integer"/>
  <xs:attributeGroup ref="personattr2"/>
</xs:attributeGroup>,
<xs:attributeGroup xmlns:xs="http://www.w3.org/2001/XMLSchema" name="personattr2">
  <xs:attribute name="attr12" type="xs:string"/>
  <xs:attribute name="attr22" type="xs:integer"/>
  <xs:attributeGroup ref="personattr3"/>
</xs:attributeGroup>,
<xs:attributeGroup xmlns:xs="http://www.w3.org/2001/XMLSchema" name="personattr3">
  <xs:attribute name="attr13" type="xs:string"/>
  <xs:attribute name="attr23" type="xs:integer"/>
</xs:attributeGroup>