Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl: how to count nodes that match a pattern

Reply
Thread Tools

xsl: how to count nodes that match a pattern

 
 
Andy Fish
Guest
Posts: n/a
 
      02-14-2005
Hi,

I'm trying to use <xsl:number> to generate a sequence number of all nodes
that match a particular pattern, e.g:

<xsl:template match="foo">
<xsl:copy>
<xsl:attribute name="id"><xsl:number level="any"/></xsl:attribute>
</xsl:copy>
</xsl:template>

this will consecutively number all the <foo> nodes in a document. regardless
of where they appear. However, in my case I have a second pattern:

<xsl:template match="foo[@type='bar']" priority="1">
</xsl:template>

Now, nodes that match the second template are included when generating the
number for the first template, so the numbering within the output document
is no longer consecutive.

I realise that in this trivial case I could have used [@type <> 'bar'] to
explicitly exclude some nodes from the numbering but I'm trying to figure
out if there is a simple way to just say 'count all nodes that are processed
by this pattern' (like if I was using an imperative language I would just
increment a variable every time I output the id attribute)

TIA

Andy



 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      02-14-2005

but I'm trying to figure
out if there is a simple way to just say 'count all nodes that are processed
by this pattern'

No.

(like if I was using an imperative language I would just
increment a variable every time I output the id attribute)

that would be (potentially) a different number again of course, counting
how many times the template is executed would also count any executions
that generate nodes into a variable rather into the final result tree.

David
 
Reply With Quote
 
 
 
 
Sebastian Millies
Guest
Posts: n/a
 
      02-16-2005
Am Mon, 14 Feb 2005 14:20:06 GMT schrieb David Carlisle:

> but I'm trying to figure
> out if there is a simple way to just say 'count all nodes that are processed
> by this pattern'
>
> No.
>

is there non-simple but general way, meaning a
pure XSLT 1.0 solution using parameterized templates,
but no XSLT extensions?

I'm not sure this question is sufficiently precise
to admit of a precise answer ... [Smullyan]

-- Sebastian
 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      02-16-2005
Sebastian Millies <REPLACE-OBVIOUSsDOTmilliesATidsMINUSscheerDOTde> writes:

> Am Mon, 14 Feb 2005 14:20:06 GMT schrieb David Carlisle:
>
> > but I'm trying to figure
> > out if there is a simple way to just say 'count all nodes that are processed
> > by this pattern'
> >
> > No.
> >

> is there non-simple but general way, meaning a
> pure XSLT 1.0 solution using parameterized templates,
> but no XSLT extensions?


I think you'd have to say No to that as well.
Given an existing stylesheet the way to answer the question about how
often a particular template is run would be to use some external tracing
or profiling tool. Eg if you are using saxon the output of saxon -T
would tell you that soon enough.

>
> I'm not sure this question is sufficiently precise
> to admit of a precise answer ... [Smullyan]
>
> -- Sebastian


David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a pattern using a previous match and a count of the numberof '('s in it me Python 0 01-27-2009 12:02 PM
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
finding nodes that don't match other nodes mlybarger@gmail.com XML 2 01-27-2005 07:26 PM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments