Re: [xsl] Getting a specific element count from a generic match

Subject: Re: [xsl] Getting a specific element count from a generic match
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Fri, 7 Apr 2006 13:50:57 -0500
Hi, Spencer,

A thought: Create a variable with the names of all the nodes in the input
file (removing duplicates along the way, of course). Then, for each name
entry in your variable, see how many match that name.

Something like this (tested with Saxon 8.6.1 - yes, I need to remember to
upgrade):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:variable name="namelist" select="//*[not(name() =
following::*/name())]"/>

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

  <xsl:template match="/">
    <xsl:for-each select="$namelist">
      <xsl:variable name="thisname" select="name()"/>
      <name value="{$thisname}" count="{count($root//*[name() =
$thisname])}"/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

If your input file is very large, this idea will be a problem, because it
uses // twice, but it'll do for small files.

Jay Bryant
Bryant Communication Services


----- Original Message ----- 
From: "Spencer Tickner" <spencertickner@xxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, April 07, 2006 11:33 AM
Subject: [xsl] Getting a specific element count from a generic match


Hi eveyone,

Thanks in advance for the help. I have a generic match in my xsl that
references another xml lookup file (not the problem). I need to count
the number of preceeding-sibiling:: elements with the same name. The
problem is the template that it is matching on is generic (the
template must remain a generic * match. ).

XSL (Doesn't work - Expected node test in pattern error)

<xsl:template match="*" mode="lookup">
<!-- I know the line below is incorrect but I think it illustrates
what I'm trying to accomplish -->
<xsl:number count="preceding-sibling::*[name()]" format="(1)"/>
</xsl:template>


Sooo if I had a source document like

<foo>
  <bar>Test</bar>
  <bar>Test</bar>
  <foo>
     <sab>Test</sab>
      <rab>Test</rab>
  </foo>
</foo>


When the template matched bar[1] produces (1)
When the template matched bar[2] produces (2)
When the template matched foo/rab[1] produces (1)

And the pattern continues ...

Any thoughts,

Spence

Current Thread