XSL 查找具有同级键的节点

XSL find nodes that have the key as sibling

我有这个(很奇怪)XML,来自 4 个家庭的 7 个 children。 有 5 个男孩有 6 个苹果和 9 个橙子,有 2 个女孩有 3 个苹果和 3 个橙子。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fruits.xsl"?>
<report>
    <family>
        <gender>
            <boyorgirl>Boy</boyorgirl>
                <person>
                    <apples>1</apples>
                    <oranges>1</oranges>
                    <id>1</id>
                </person>
        </gender>
        <gender>
            <boyorgirl>Girl</boyorgirl>
                <person>
                    <apples>2</apples>
                    <oranges>0</oranges>
                    <id>2</id>
                </person>
        </gender>
        <gender>
            <boyorgirl>Boy</boyorgirl>
                <person>
                    <apples>1</apples>
                    <oranges>4</oranges>
                    <id>3</id>
                </person>
        </gender>
    </family>
    <family>
        <gender>
            <boyorgirl>Girl</boyorgirl>
                <person>
                    <apples>1</apples>
                    <oranges>3</oranges>
                    <id>4</id>
                </person>
        </gender>
    </family>
    <family>
        <gender>
            <boyorgirl>Boy</boyorgirl>
                <person>
                    <apples>1</apples>
                    <oranges>0</oranges>
                    <id>5</id>
                </person>
        </gender>
    </family>
    <family>
        <gender>
            <boyorgirl>Boy</boyorgirl>
                <person>
                    <apples>2</apples>
                    <oranges>2</oranges>
                    <id>6</id>
                </person>
                <person>
                    <apples>1</apples>
                    <oranges>2</oranges>
                    <id>7</id>
                </person>
        </gender>
    </family>
</report>

我想数一数每个性别有多少人,他们总共有多少个苹果和橘子。 我不想在现代对性别进行硬编码,以防有人声称他们的性别不是男孩或女孩。预期输出为

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fruitcounting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <fruitline>
        <boyorgirl>Boy</boyorgirl>
        <numberOfPersons>5</numberOfPersons>
        <apples>6</apples>
        <oranges>9</oranges>
    </fruitline>
    <fruitline>
        <boyorgirl>Girl</boyorgirl>
        <numberOfPersons>2</numberOfPersons>
        <apples>3</apples>
        <oranges>3</oranges>
    </fruitline>
</fruitcounting>

我有一个样式表的开头,但它只计算第一个家庭中的成员和水果 - 我如何更改它以便它找到所有 boys/girls,而不考虑家庭?或者,我想我可以将其表述为具有前同级键的所有节点。

我只能使用没有扩展的 XSL 1.0

<?xml version="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="fruits" match="gender" use="boyorgirl"/>

<xsl:template match="/">
    <fruitcounting>

        <xsl:for-each select="//gender[generate-id(.)=generate-id(key('fruits', boyorgirl)[1])]">

            <fruitline>
                <boyorgirl><xsl:value-of select="boyorgirl"/></boyorgirl>
                <numberOfPersons><xsl:value-of select="count(../gender/person/id)"/></numberOfPersons>
                <apples><xsl:value-of select="sum(../gender/person/apples)"/></apples>
                <oranges><xsl:value-of select="sum(../gender/person/oranges)"/></oranges>

            </fruitline>

        </xsl:for-each>
    </fruitcounting>
</xsl:template>

</xsl:stylesheet>

您可以将 <numberOfPersons><xsl:value-of select="count(../gender/person/id)"/></numberOfPersons> 替换为 <numberOfPersons><xsl:value-of select="count(key('fruits', boyorgirl)/person)"/></numberOfPersons> 并以相同的方式使用 key 函数来识别您的组,例如<apples><xsl:value-of select="sum(key('fruits', boyorgirl)/person/apples)"/></apples> 在其他计算中。

所以完整的代码变成了

<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="fruits" match="gender" use="boyorgirl"/>

<xsl:template match="/">
    <fruitcounting>

        <xsl:for-each select="//gender[generate-id(.)=generate-id(key('fruits', boyorgirl)[1])]">

            <fruitline>
                <boyorgirl><xsl:value-of select="boyorgirl"/></boyorgirl>
                <numberOfPersons><xsl:value-of select="count(key('fruits', boyorgirl)/person)"/></numberOfPersons>
                <apples><xsl:value-of select="sum(key('fruits', boyorgirl)/person/apples)"/></apples>
                <oranges><xsl:value-of select="sum(key('fruits', boyorgirl)/person/oranges)"/></oranges>

            </fruitline>

        </xsl:for-each>
    </fruitcounting>
</xsl:template>

</xsl:stylesheet>

在线 http://xsltransform.net/bwdwsb