如何在xslt中创建摘要列表?

时间:2015-09-30 23:02:42

标签: xslt xpath xslt-2.0

我在为HTML表创建XSLT模板时遇到问题。我有以下XML数据,我需要创建一个摘要。

<doc-number>1</doc-number>
<book>Gardening/John Smith</book>
<call-no>B-1</call-no>

<doc-number>1</doc-number>
<book>Gardening/John Smith</book>
<call-no>B-1/23</call-no>

<doc-number>1</doc-number>
<book>Forest/Ema Who</book>
<call-no>A-1/2</call-no>

<doc-number>1</doc-number>
<book>Gardening/John Smith</book>
<call-no>B-1/5</call-no>

我需要这个:

Items  Book
3 Gardening/John Smith         
1 Forest/Ema Who    

我认为它可能与current-grouping-key有关,但我不能应用它。你知道吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

<强> XML:

<root>
    <doc-number>1</doc-number>
    <book>Gardening/John Smith</book>
    <call-no>B-1</call-no>

    <doc-number>1</doc-number>
    <book>Gardening/John Smith</book>
    <call-no>B-1/23</call-no>

    <doc-number>1</doc-number>
    <book>Forest/Ema Who</book>
    <call-no>A-1/2</call-no>

    <doc-number>1</doc-number>
    <book>Gardening/John Smith</book>
    <call-no>B-1/5</call-no>
</root>

<强> XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="root">
      <xsl:text>Items&#160;&#160;&#160;Books&#10;</xsl:text>
      <xsl:for-each-group select="book" group-by=".">
        <xsl:value-of select="count(current-group())"/>&#160;&#160;&#160;<xsl:value-of select="current-grouping-key()"/>
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

<强>输出:

Items   Books
3   Gardening/John Smith
1   Forest/Ema Who