[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Assigning serial numbers

Subject: RE: Assigning serial numbers
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 10 Apr 2010 10:30:59 +0100
RE:  Assigning serial numbers
> <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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.