xsl-list
[Top] [All Lists]

[xsl] Assigning serial numbers

2010-04-13 00:14:32
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(_at_)lists(_dot_)mulberrytech(_dot_)com>
From: "Michael Kay" <mike(_at_)saxonica(_dot_)com>
Subject: RE: [xsl] Assigning serial numbers
Message-ID: <08A6868B7F1D4D8094D70747568B5DFD(_at_)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

------------------------------
>




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>