xsl: how to count nodes that match a pattern

Discussion in 'XML' started by Andy Fish, Feb 14, 2005.

  1. Andy Fish

    Andy Fish Guest

    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
    Andy Fish, Feb 14, 2005
    #1
    1. Advertising

  2. 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
    David Carlisle, Feb 14, 2005
    #2
    1. Advertising

  3. 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
    Sebastian Millies, Feb 16, 2005
    #3
  4. 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
    David Carlisle, Feb 16, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Tayek
    Replies:
    3
    Views:
    515
    Ray Tayek
    Nov 30, 2003
  2. Replies:
    2
    Views:
    395
  3. Replies:
    1
    Views:
    3,599
    A. Bolmarcich
    May 27, 2005
  4. David Cater
    Replies:
    6
    Views:
    1,773
    Pavel Lepin
    May 31, 2007
  5. me
    Replies:
    0
    Views:
    258
Loading...

Share This Page