[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
[Recent Entries]
[Reply To This Message]
Assigning serial numbers
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: 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
------------------------------

|
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format
RSS 2.0 |
|
Atom 0.3 |
|
|