0
<result>
    <account id="123456">
        <missing_data missing_in="Archimede"/>
            <line_not_found db_table="PRODUCT" id="PDT">
            </line_not_found>
            <line_not_found db_table="ADRESS" id="ADR">
            </line_not_found>
        </missing_data>
        <missing_data missing_in="Cassiope">
            <line_not_found db_table="PRODUCT" id="PDT">
            </line_not_found>
            <line_not_found db_table="PRODUCT" id="PDT">
            </line_not_found>
            <line_not_found db_table="ADRESS" id="ADR">
            </line_not_found>
            <line_not_found db_table="ADRESS" id="ADR">
            </line_not_found>
            <line_not_found db_table="ADRESS" id="ADR">
            </line_not_found>
            <line_not_found db_table="ADRESS" id="ADR">
            </line_not_found>
        </missing_data>
    </account>
</result>

我想对 missing_data@missing_in 和 line_not_found@id 进行分组和计数

例如,在这种情况下: 阿基米德 - PDT : 1 阿基米德 - ADR : 1 仙后座 - PDT : 2 仙后座 - ADR : 4

我不得不承认我是 XSLT 的初学者。我试图从Xslt distinct select / Group by中获得灵感,但问题并不完全相同,因为我的密钥是复合的,但来自多个 XML 标记。

我的 XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:key name="missing_data_key" match="line_not_found" use="concat(../@missing_in, ',', @id)"/>
    <xsl:template match="/">
    <xsl:for-each select="result/account">
        <xsl:value-of select="@id" />
        <xsl:for-each select="missing_data/line_not_found[
    count(
        .|key('missing_data_key', @id)[1]
        ) = 1
    ]
">
            <ul>
                <xsl:value-of select="@id"/>
    ( absent dans 
    <xsl:value-of select="../@missing_in"/>
    )
    <xsl:value-of select="' - '"/>
                <xsl:value-of select="
        count(
            key('missing_data_key', ../@missing_in)[
                count(
                  key('missing_data_key', concat(../@missing_in,',',@id))[1]
                ) = 1
            ]
        )
      "/>
            </ul>
        </xsl:for-each>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

XSLT 1 在这个项目中是强制性的

提前致谢

4

1 回答 1

2

因为您在 XSLT 1.0 中对元素进行分组,所以您必须使用 Muenchian Grouping 来实现您想要做的事情:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="text" />

    <!-- Use the following key to index all line_not_found elements
        in the document by their id -->
    <xsl:key name="line-key"
             match="missing_data/line_not_found"
             use="@id" />

    <xsl:template match="missing_data">
        <!-- Obtain information from current node before losing the
            context -->
        <xsl:variable name="id" select="generate-id()" />
        <xsl:variable name="location" select="@missing_in" />
        <!-- Obtain the first element of each group which parent is the current missing_data element  -->
        <xsl:for-each select="line_not_found[generate-id() = generate-id(key('line-key', @id)[generate-id(..) = $id][1])]">
            <!-- Counts the subset of line_not_found elements which are children of
                 the current missing_data parent (put into in a variable for clarity) -->
            <xsl:variable name="count-children" select="count(key('line-key', @id)[generate-id(..) = $id])" />
            <xsl:value-of select="concat($location, ' - ', @id, ' : ', $count-children, ' ')" />
        </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>
于 2013-02-28T10:37:54.397 回答