XSLT 1.0 由多个子节点分组的父节点分组

XSLT 1.0 Grouping of parent node grouped by multiple child nodes

我正在尝试基于多个子节点进行分组,然后显示由子节点分组的父节点。我已经缩小了一些例子,但我希望你明白了。现在我已经在 XSL 2.0 中使用它,但发现我只能在此应用程序中使用 1.0。我一直在阅读 Muenchian 分组方法,但似乎无法弄明白。

这是XML的(部分):

<Persons>
    <Person PersonID="3987">
        <Desks>
            <Desk Name="10" Active="true">
            </Desk>
            <Desk Name="11" Active="true">
            </Desk>
        </Desks>
    </Person>
    <Person PersonID="3883">
        <Desks>
            <Desk Name="10" Active="true">
            </Desk>
            <Desk Name="11" Active="true">
            </Desk>
            <Desk Name="12" Active="true">
            </Desk>
            <Desk Name="13" Active="true">
            </Desk>
        </Desks>
    </Person>
</Persons>

这是我对 Muenchian 分组方法的了解:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:key name="persons-per-desk" match="Person" use="Desks/Desk/@Name"/>

    <xsl:template match="Person[generate-id() = generate-id(key('persons-per-desk', Desks/Desk/@Name)[1])]">
        <Desk>
            <xsl:copy-of select="Desks/Desk/@Name" />
            <Person>
                <xsl:copy-of select="key('persons-per-desk', Desks/Desk/@Name)/@PersonID" />
            </Person>
        </Desk>
    </xsl:template>

    <xsl:template match="Person" />
</xsl:stylesheet>

我希望得到的:

<Desk Name="10">
   <Person PersonID="3883"/>
   <Person PersonID="3987"/>
</Desk>
<Desk Name="11">
   <Person PersonID="3883"/>
   <Person PersonID="3987"/>
</Desk>
<Desk Name="12">
   <Person PersonID="3883"/>
</Desk>
<Desk Name="13">
   <Person PersonID="3883"/>
</Desk>

我得到的:

<Desk Name="11">
   <Person PersonID="3883"/>
</Desk>

如有任何帮助,我们将不胜感激。

您正在尝试按办公桌对人员进行分组 name/s - 但您需要做的是先按姓名对办公桌进行分组,然后列出每组中拥有办公桌的人员:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="desk-by-name" match="Desk" use="@Name"/>

<xsl:template match="/Persons">
    <Desks>
        <!-- create a grpup for each distinct desk name -->
        <xsl:for-each select="Person/Desks/Desk[generate-id() = generate-id(key('desk-by-name', @Name)[1])]">
            <Desk Name="{@Name}">
                <!-- list the persons having desks in this group -->
                <xsl:for-each select="key('desk-by-name', @Name)/ancestor::Person">
                    <Person PersonID="{@PersonID}"/>
                </xsl:for-each>
            </Desk>
        </xsl:for-each>
    </Desks>
</xsl:template>

</xsl:stylesheet>