[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] how do you count based on the number of preceding attributes?


Subject: RE: [xsl] how do you count based on the number of preceding attributes?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 6 Jun 2008 21:47:06 +0100

> Using xsl processor: Saxon 8B, here's my test data:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>     <div0 type="heading" id="abc.div.1.2.3">
>         <div1 type="article" id="abc.div.1.2.6">
>             <div2 type="subsection" id="abc.div.1.2.1"/>
>             <pb n="1" id="abc.1.2.2"/>
>             <div2 type="subsection" id="abc.div.1.2.64"/>
>         </div1>
>     </div0>
> </root>
> 
> Here's the stylesheet I'm working on:
> 
[identity template]+
> 
>     <xsl:template match="*/@id[contains(.,'div')]">

The "*/" adds nothing (well, it says "only match id attributes if they have
an element as a parent"). You can drop it.
> 
>         <xsl:variable name="substring"
> select="substring-after(substring-after(substring-after(substr
> ing-after(.,'.'),'.'),'.'),'.')"/>

You don't seem to use this variable and I don't think you need it.

>         <xsl:variable name="test_value"><xsl:value-of 
> select="count(preceding::node())"/></xsl:variable>

NEVER write variables like this. It should be 

<xsl:variable name="test_value" select="string(count(preceding::node()))"/>

You don't want a document node containing a text node; you want the integer,
converted to a string. 

> I want the value of $test_value to be, for example, 1 if the @id
containing the string 'div' is the first @id, 2 if it is the second, and so
on.

You haven't made it clear whether you want to count all @id attributes, or
only those that contain 'div'. In the first case it's
count(preceding::*[@id]), in the second it's
count(preceding::*[contains(@id, 'div')]).

Alternatively, use <xsl:number level="any" count="*[@id]"/> 

>         <xsl:attribute name="id"
> select="concat(replace(.,'([a-z]{3})\.div\.([0-9]+)\.([0-9]+)\
> .([0-9]+)','$1.div.$2.$3.'),$test_value)"/>

The seems very cumbersome. To replace the last number in the string, use

replace(., '[0-9]+$', $test_value)

Michael Kay
http://www.saxonica.com/


Current Thread
Keywords
xsl