RE: [xsl] Grouping over multiple files

Subject: RE: [xsl] Grouping over multiple files
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Sat, 14 Jan 2006 16:21:27 +0100 (CET)
Kevin Bird wrote:

> The grouping is working fine, but I would like to add a source
> attribute to the revenue elements to show which file they came
> from (please see 'desired output' section below.

  You can compare the document nodes:

    <xsl:stylesheet
        version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:xs="http://www.w3.org/2001/XMLSchema";
        xmlns:k="kevin.bird"
        exclude-result-prefixes="xs k">

      <xsl:output indent="yes" method="xml"/>

      <xsl:variable name="north" select="document('k-north.xml')"/>
      <xsl:variable name="south" select="document('k-south.xml')"/>
      <xsl:variable name="east"  select="document('k-east.xml')"/>
      <xsl:variable name="west"  select="document('k-west.xml')"/>

      <xsl:variable name="northsales"
                    select="$north/sales/period/person"/>
      <xsl:variable name="southsales"
                    select="$south/sales/period/person"/>
      <xsl:variable name="eastsales"
                    select="$east/sales/period/person"/>
      <xsl:variable name="westsales"
                    select="$west/sales/period/person"/>

      <xsl:function name="k:get-source" as="xs:string">
        <xsl:param    name="node" as="node()"/>
        <xsl:variable name="root" as="node()"
                      select="root($node)"/>
        <xsl:choose>
          <xsl:when test="$root = $north">
            <xsl:text>north</xsl:text>
          </xsl:when>
          <xsl:when test="$root = $south">
            <xsl:text>south</xsl:text>
          </xsl:when>
          <xsl:when test="$root = $east">
            <xsl:text>east</xsl:text>
          </xsl:when>
          <xsl:when test="$root = $west">
            <xsl:text>west</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">
              <xsl:text>Unjnown source!</xsl:text>
            </xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:function>

      <xsl:template match="/">
        <combinedsales>
          <xsl:for-each-group
              select="$northsales, $southsales, $eastsales, $westsales"
              group-by="name">
            <person>
              <xsl:copy-of select="(current-group()/name)[1]"/>
              <xsl:for-each select="current-group()/revenue">
                <xsl:copy>
                  <xsl:attribute name="source"
                                 select="k:get-source(.)"/>
                  <xsl:copy-of select="@*"/>
                  <xsl:copy-of select="node()"/>
                </xsl:copy>
              </xsl:for-each>
            </person>
          </xsl:for-each-group>
        </combinedsales>
      </xsl:template>

    </xsl:stylesheet>

  This produces:

    <?xml version="1.0" encoding="UTF-8"?>
    <combinedsales>
       <person>
          <name>Peter</name>
          <revenue source="north">1000</revenue>
          <revenue source="south">500</revenue>
          <revenue source="east">40</revenue>
       </person>
       <person>
          <name>Fred</name>
          <revenue source="north">2000</revenue>
          <revenue source="east">5000</revenue>
          <revenue source="west">400</revenue>
       </person>
       <person>
          <name>Alex</name>
          <revenue source="north">100</revenue>
          <revenue source="east">100</revenue>
          <revenue source="west">2000</revenue>
       </person>
       <person>
          <name>Barney</name>
          <revenue source="north">20</revenue>
          <revenue source="south">750</revenue>
       </person>
    </combinedsales>

--drkm


















	

	
		
___________________________________________________________________________ 
Nouveau : tiliphonez moins cher avec Yahoo! Messenger ! Dicouvez les tarifs exceptionnels pour appeler la France et l'international.
Tilichargez sur http://fr.messenger.yahoo.com

Current Thread