xsl: how to count nodes that match a pattern

A

Andy Fish

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
 
D

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.

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

Sebastian Millies

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
 
D

David Carlisle

Sebastian Millies said:
Am Mon, 14 Feb 2005 14:20:06 GMT schrieb David Carlisle:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top