像 sql group-by 一样将两个 xml 文件分组 [英] Groups two xml files like a sql group-by

查看:32
本文介绍了像 sql group-by 一样将两个 xml 文件分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With the following file as input of xsltprocessor :

mylibrary.xml :

mylibrary.xml :

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

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

and this one that can be used : 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 类别中获得的图书编号.

i want to get the numbers of book i got in mylibrary, groupby category, using xslt 1-0.

想要的输出:

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

这是我使用 Martin Honnen 方法编写的 xsl,在使用 2 个不同的 XML 文件作为源时进行分组?"中解释了这一点.,我认为可以解决问题,但我还没有验证,也许有人有更好的解决方案.

here is the xsl i write using Martin Honnen method explains in 'Grouping when using 2 different XML files as sources?' , i think that solve the problem but i do not validate yet and perhaps someone have a better solution.

<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>

推荐答案

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

here is the xsl i write using Martin Honnen method explains in 'Grouping when using 2 different XML files as sources?' , i think that solve the problem but i do not validate yet and perhaps someone have a better solution

.

这是一个更简单的 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 文档时:

When applied on the provided XML document contained in MyLibrary.xml:

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

并将此提供的 XML 文档包含在文件 C:\temp\delete\reference.xml 中:

and having this provided XML document contained in the file 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 解决方案:

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

This is slightly simpler, as we can define the key to be dependent on the second document.

<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>

这篇关于像 sql group-by 一样将两个 xml 文件分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆