Xslt 将两个xml文件按sql分组

Xslt 将两个xml文件按sql分组,xslt,xslt-1.0,xslt-grouping,Xslt,Xslt 1.0,Xslt Grouping,使用以下文件作为xsltprocessor的输入: mylibrary.xml: <library> <book isbn="1"/> <book isbn="3"/> <book isbn="5"/> </library> 下面是我使用Martin Honnen方法编写的xsl,在“使用两个不同的XML文件作为源时进行分组”中进行了解释, 我认为这可以解决问题,但我还没有证实,也许有人有更好的解决方

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

mylibrary.xml:

<library>  
   <book isbn="1"/>    
   <book isbn="3"/>
   <book isbn="5"/>
</library>  
下面是我使用Martin Honnen方法编写的xsl,在“使用两个不同的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>

: 
书籍
下面是我使用Martin Honnen方法编写的xsl,在 “当使用两个不同的XML文件作为源时进行分组?”我认为 解决问题,但我还没有验证,也许有人有 更好的解决方案

这里有一个更简单的XSLT1.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>
<library>
    <book isbn="1"/>
    <book isbn="3"/>
    <book isbn="5"/>
</library>
<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>
并将此提供的XML文档包含在文件
C:\temp\delete\reference.XML

<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>
<library>
    <book isbn="1"/>
    <book isbn="3"/>
    <book isbn="5"/>
</library>
<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>
II。XSLT2.0解决方案:

<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>
<library>
    <book isbn="1"/>
    <book isbn="3"/>
    <book isbn="5"/>
</library>
<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>
这稍微简单一些,因为我们可以定义依赖于第二个文档的键

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

: 
书籍和#xA;

@\u seb:这是我连续第二次给你更好的答案——希望你能清楚地意识到这一点。顺便说一句,+1让你原来的问题变得有趣。@_dimitre:当然,我非常感谢你的帮助,我知道你花了宝贵的时间帮助我。这绝对是最好的方法和解决方案!!!非常感谢您的高技能水平。