【问题标题】:Groups two xml files like a sql group-by像 sql group-by 一样对两个 xml 文件进行分组
【发布时间】:2012-01-10 04:28:40
【问题描述】:

使用以下文件作为 xsltprocessor 的输入:

mylibrary.xml:

<library>  
   <book isbn="1"/>    
   <book isbn="3"/>
   <book isbn="5"/>
</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、groupby 类别中获得的书籍数量。

想要的输出:

SF : 2 book(s) 
Comedy : 1 book(s) 

这是我使用 Martin Honnen 方法编写的 xsl,在“使用 2 个不同的 XML 文件作为源时分组?” , 我认为这可以解决问题,但我尚未验证,也许有人有更好的解决方案。

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl"
  version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:key name="book-by-category" match="book" use="category"/>

<xsl:param name="bookref" select="'bookreference.xml'"/>
<xsl:variable name="doc" select="document($bookref)/reference"/>

<xsl:variable name="rtf">
    <xsl:apply-templates select="//library" mode="merge"/>
</xsl:variable>

<xsl:template match="library" mode="merge">
    <xsl:element name="mybookcat">
    <xsl:for-each select="book">
        <xsl:variable name="isbn" select="@isbn"/>
        <xsl:element name="book">
        <xsl:attribute name="isbn"><xsl:value-of select="$isbn"/></xsl:attribute>
        <xsl:for-each select="$doc/book[@isbn=$isbn]">
            <xsl:element name="category">
                <xsl:value-of select="./category"/>
            </xsl:element>
        </xsl:for-each>
        </xsl:element>
    </xsl:for-each>
    </xsl:element>
</xsl:template>     

<xsl:template match="/">
    <xsl:apply-templates select="exsl:node-set($rtf)/mybookcat"/>
</xsl:template>

<xsl:template match="mybookcat">
<xsl:for-each select="book[count(. | key('book-by-category',category)[1]) = 1]"> 
    <xsl:sort select="category"/> 
    <xsl:value-of select="category" /><xsl:text> : </xsl:text>
    <xsl:variable name="current-cat" select="key('book-by-category', category)"/>
    <xsl:value-of select="count($current-cat)"/><xsl:text> book(s)
</xsl:text>
    <xsl:for-each select="key('book-by-category', category)">
        <xsl:sort select="@isbn" data-type="number" />
        <xsl:value-of select="@isbn" /><xsl:text>
</xsl:text>            
    </xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • @_seb:这是我连续第二次给你更好的答案——希望你很清楚这一点。顺便说一句,+1 使您的原始问题的替代方案也很有趣。
  • @_dimitre : 当然,我真的很感谢你的帮助,我知道你花在帮助上的宝贵时间。

标签: xslt xslt-1.0 xslt-grouping


【解决方案1】:

这是我使用 Martin Honnen 方法编写的 xsl,在 “使用 2 个不同的 XML 文件作为源时进行分组?” , 我觉得 解决问题,但我还没有验证,也许有人有 更好的解决方案

.

这是一个更简单的 XSLT 1.0 解决方案,它不使用任何扩展函数或xsl:for-each:

<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: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:value-of select="count($vBooksinCat[@isbn=$vMyIsbns])"/>
         <xsl:text> book(s)&#xA;</xsl:text>
     </xsl:template>
     <xsl:template match="text()"/>
</xsl:stylesheet>

应用于MyLibrary.xml中所提供的XML文档时:

<library>
    <book isbn="1"/>
    <book isbn="3"/>
    <book isbn="5"/>
</library>

并将此提供的 XML 文档包含在文件 C:\temp\delete\reference.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 : 2 book(s)
Comedy : 1 book(s)

二。 XSLT 2.0 解决方案:

这稍微简单一些,因为我们可以定义依赖于第二个文档的键。

<xsl:stylesheet version="2.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[@isbn = $vMyIsbns]"
          use="category"/>

     <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:value-of select="count($vBooksinCat)"/>
         <xsl:text> book(s)&#xA;</xsl:text>
     </xsl:template>
     <xsl:template match="text()"/>
</xsl:stylesheet>

【讨论】:

  • 绝对是最好的方法和解决方案!!!非常感谢你的高水平。
猜你喜欢
  • 2012-01-11
  • 2013-02-20
  • 2013-01-27
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多