[xsl] Assigning serial numbers

Subject: [xsl] Assigning serial numbers
From: "Siddhi Thakkar" <siddhi.thakkar@xxxxxxxxxxxxxx>
Date: Tue, 13 Apr 2010 10:44:07 +0530
Thanks for the valuable inputs Michael. I will surely keep these in mind.

---------
---------------------

Date: Sat, 10 Apr 2010 10:30:59 +0100 To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> From: "Michael Kay" <mike@xxxxxxxxxxxx> Subject: RE: [xsl] Assigning serial numbers Message-ID: <08A6868B7F1D4D8094D70747568B5DFD@Sealion>


<xsl:template match="//a"> <xsl:for-each select="@href"> <xsl:if test="not(for $x in //@id return $x[$x=current()])">

This is very peculiar coding!


(a) Don't write match="//a", write match="a". The meaning is 99% the same
except (i) the precedence of the rule is different, and (ii) the second form
is cheaper because it doesn't have to check that the element is part of a
document tree.


(b) It's odd to do a for-each over a singleton.

(c) The construct (for $x in EXP return $x[PRED]) is equivalent to EXP[PRED]

So you could write

<xsl:template match="a">
 <xsl:if test="not(//@id[. = current()/@href])">

(d) For repeated searches like this it's best to define a key:

<xsl:key name="k" match="*" use="@id"/>

then

<xsl:template match="a
<xsl:if test="not(key('k', @href))">

<!-- This is where I would like to write the code to assign serial
number-->

The simplest way to do this is to only select the <a> elements you want to
include in the table; then position() gives you what you need:

<xsl:key name="k" match="*" use="@id"/>

<xsl:template match="/">
      <table>
          <xsl:apply-templates select="//a[not(key('k', @href))]">
       </table>
</xsl:template>

<xsl:template match="a">
   <tr>
     <td>
       <xsl:value-of select="position()"/>
     </td>
     <td>
       <xsl:value-of select="@href"/>
     </td>
   </tr>
</xsl:template>

Regards,

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

------------------------------

Current Thread