2

这是我在这里提出的问题的演变:将 两个 xml 文件分组,如 sql group-by 给出的示例和 Dimitre 解决方案正在计算不同的 isbn 值。现在修改库 xml 以具有 mylibrary.xml :

<library>  
   <book id="1" isbn="1"/>
   <book id="2" isbn="1"/>
   <book id="3" isbn="2"/>
   <book id="4" isbn="4"/>
   <book id="5" isbn="5"/>
   <book id="6" isbn="4"/>
   <book id="7" isbn="4"/>   
</library>  

还有这个可以使用的:bookreference.xml:

<reference>  
    <book isbn="1">  
        <category>SF</category>  
    </book>  
    <book isbn="2">  
        <category>SF</category>  
    </book>  
    <book isbn="3">  
        <category>SF</category>  
    </book>  
    <book isbn="4">  
        <category>Comedy</category>  
    </book>  
    <book isbn="5">  
        <category>Comedy</category>  
    </book>
</reference>  

我想使用 xslt 1-0 获取我在 mylibrary 中得到的书的数量“即使有些具有相同的 isbn”,groupby 类别。

想要的输出:

SF : 3 book(s) 
Comedy : 4 book(s) 

我的 xslt 在这里建议:像 sql group-by 一样将两个 xml 文件分组工作正常,但当然使用“for-each”循环和扩展函数。当然有更好的解决方案。

4

3 回答 3

1

修改 Dimitri 版本为此工作

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="kBookByCat" match="book" use="category"/>

<xsl:variable name="vRef" select="document('file:///c:/temp/delete/reference.xml')"/>

<xsl:variable name="meh" select="*"/>

<xsl:template match="/">
    <xsl:apply-templates select="$vRef/reference/book[generate-id()=generate-id(key('kBookByCat', category)[1])]" />
</xsl:template>

<xsl:template match="book">
    <xsl:variable name="cat" select="category"/>
    <xsl:value-of select="category"/> : <xsl:text/>
    <xsl:variable name="isbns" select="$vRef/reference/book[category=$cat]/@isbn"/>
    <xsl:value-of select="count($meh/book[@isbn=$isbns])"/>
    <xsl:text> book(s)&#xA;</xsl:text>
</xsl:template>
于 2011-12-02T14:00:11.670 回答
1

又是一个很好的问题!(+1)

这种转变,使用两个关键来实现全效率

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

     <xsl:key name="kBookByCat" match="book"
          use="category"/>

     <xsl:key name="kBookByIsbn" match="book"
          use="@isbn"/>

     <xsl:variable name="vDoc" select="/"/>

     <xsl:variable name="vRef" select=
     "document('file:///c:/temp/delete/reference.xml')"/>

     <xsl:variable name="vMyIsbns" select="/*/*/@isbn"/>

     <xsl:variable name="vResult">
      <xsl:apply-templates select="$vRef/*"/>
     </xsl:variable>

     <xsl:template match="/">
      <xsl:copy-of select="$vResult"/>
     </xsl:template>

     <xsl:template match=
      "book[generate-id()
           =
            generate-id(key('kBookByCat', category)[1])
            ]
      ">
         <xsl:variable name="vBooksinCat" select=
              "key('kBookByCat', category)"/>

         <xsl:value-of select="category"/> : <xsl:text/>
         <xsl:for-each select="$vDoc">
           <xsl:value-of select="count(key('kBookByIsbn',$vBooksinCat/@isbn))"/>
         </xsl:for-each>
         <xsl:text> book(s)&#xA;</xsl:text>
     </xsl:template>
     <xsl:template match="text()"/>
</xsl:stylesheet>

当应用于包含在文件 mylibrary.xml 中提供的 XML 文档时

<library>
   <book id="1" isbn="1"/>
   <book id="2" isbn="1"/>
   <book id="3" isbn="2"/>
   <book id="4" isbn="4"/>
   <book id="5" isbn="5"/>
   <book id="6" isbn="4"/>
   <book id="7" isbn="4"/>
</library>

并在 C:\temp\delete\reference.xml 中提供此 XML 文档

<reference>
    <book isbn="1">
        <category>SF</category>
    </book>
    <book isbn="2">
        <category>SF</category>
    </book>
    <book isbn="3">
        <category>SF</category>
    </book>
    <book isbn="4">
        <category>Comedy</category>
    </book>
    <book isbn="5">
        <category>Comedy</category>
    </book>
</reference>

产生所需的正确输出

SF : 3 book(s)
Comedy : 4 book(s)
于 2011-12-02T14:09:27.240 回答
0

除了伟大的 dimitri 响应之外,我建议不要打印在 mylibrary 中设置为 0 的书籍类别:

<xsl:variable name="catname" select="category"/>
<xsl:for-each select="$vDoc">
    <xsl:variable name="cnt" select="count(key('kBookByIsbn',$vBooksinCat/@isbn))"/>
    <xsl:if test="$cnt &gt; 0">
        <xsl:value-of select="$catname"/> : 
        <xsl:text/>
        <xsl:value-of select="$cnt"/>
        <xsl:text> book(s)&#xA;</xsl:text>
    </xsl:if>
</xsl:for-each>
于 2011-12-02T19:44:07.417 回答