Thursday 19 October 2006

Counting nodes

I had a problem where I needed to assign a unique integer to each node in a tree, as it was processed; using XSLT 1.0

XSLT 1.0 doesn’t have user definable functions, the only way to return values is as output which can be captured in an

<xsl:variable name="captured"><xsl:call-template name="generate-output"/></xsl:variable>

And, naturally, the tree recursion was already generating output and could not easily be used to return a count of nodes processed so far as well.

After a couple of hours of struggling and some syntax checking with my brother Ben (for things like $node/ancestors::* - thanks bro) we have a fragment that does the trick in one line!

count($node/preceding::rule[ancestor::*[generate-id()=$ancestorid]])

This is more clearly excodessed as:

count($node/preceding::rule[count(ancestor::*[generate-id()=$ancestorid])])

and in english reads as: to find the sequence number for $node, count all preceding nodes who are contained by the common ancestor identified by $ancestorid. Without the codedicate, it would count all preceding nodes in the entire document, instead of those with a certain ancestor.

However this is not complete. preceding does not take into account open nodes, i.e. the node to which it is being applied, nor any of it’s ancestors; so this correction is added:

+ count($node/ancestor::rule) - count($ancestor/ancestor::rule)

as the number of non-counted nodes is clearly the difference between the number of ancestors of $node, and the number of ancestors of $ancestor.

The full statement is:

count($node/preceding::rule[ancestor::*[generate-id()=$ancestorid]])

+ count($node/ancestor::rule) - count($ancestor/ancestor::rule)

I leave it to the reader to produce a version that reports the total number "rule" nodes under the current ancestor.

Finally I used this recipe:


  <xsl:template name="rule-number">
    <xsl:param name="name"><xsl:message terminate="yes">rule-number called without name</xsl:message></xsl:param>
    <xsl:param name="rules-node" select="//dbam-bandwidth-rules"/>
    <!– take the first occurance of each rule (no preceding of same ancestor) –>
    <xsl:variable name="all_rules" select="$rules-node//rule[not(@name = preceding::rule[count(ancestor::*[generate-id()=generate-id($rules-node)]) &gt; 0]/@name)]"/>
    <xsl:for-each select="$all_rules">
      <xsl:if test="@name=$name"><xsl:value-of select="position()"/></xsl:if>
    </xsl:for-each>
  </xsl:template> 

Tuesday 3 October 2006

Kenneth Cope

Greater Than us All is a great album.

I just shelled out some dosh (hard earned cash) to buy the CD version having played my casstte album to oblivion.

I was interested to see some tracks were re-recorded; and it comes to this:

Do you have an original CD album you could send me?

I don’t like the new recordings. They are not what my mind has become adjusted to, and even worse it starts to follow a trend set by the mormon tabernacle choir which seems to be to put inappropriate expression into the performance.

I probably should explain this last accusation a bit more, and I’ll use the tabernacle choir to explain.

I think the choir get bored singing the same old hymns and such so they try and jazz them up and put a bit more emotion into it, but that doesn’t make it better, it only makes it weird. Oh, but its probably more fun to sing.

(Did I mention I’m finding it hard to get decent mo-tab music lately? I’m thinking of downloading all the conferences and ripping the choir out of the middle ot build up a new compilation).

I want "Mo-tab: The same boring old hymns" type stuff, you know, "like momma used to bake" so to speak.

Now you haven’t done a bad job on the new cut of Greater Than Us All, but you are following the trend; a bit more unexpected desparation or wonder in the voice, delay the vocalists entry slightly by half a beat, drag out a middle phrase note for no good reason, it drives me mad. Not every phrase of the song needs to sound like some kind of epiphany or revelation; the song has to do that as a whole, but these days each phrase is trying to compete with the whole song.

It ends up sounding like the emotional expression of some other song overlayed onto this one. (Its nice variety for folk who have sung the song to death no doubt)

Please, be a nice man and send me what you hooked me on, not this slghtly freaky stuff. I like my music straight: A CD of your original cut of Greater Than Us All.