xsl-list
[Top] [All Lists]

RE: [xsl] working out where the last() is

2006-10-06 04:19:41
Thanks to you all. That works perfectly. I had wondered about making a
single node list of the attribs I wanted, just couldn't see how select
it. I am using XSLT 2.0 so the seperator is ideal.

Thanks again.
Rob  
(robert(_dot_)walpole(_at_)devon(_dot_)gov(_dot_)uk)

-----Original Message-----
From: Michael Kay [mailto:mike(_at_)saxonica(_dot_)com] 
Sent: 06 October 2006 11:41
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] working out where the last() is


In XSLT 2.0 (with $a as the <areaserved> element):

<xsl:value-of select="$a//*[not(*)]/@name" separator=",&#160;"/>

In 1.0:

<xsl:for-each select="$a//*[not(*)]/@name">
  <xsl:value-of select="."/>
  <xsl:if test="position() != last()">,&#160;</xsl:if>
</xsl:for-each>

last() gives you the number of items in the current node 
list, which in this
case is the nodes selected by 
$a//*[not(*)]/@name.

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

-----Original Message-----
From: Robert Walpole 
[mailto:robert(_dot_)walpole(_at_)devon(_dot_)gov(_dot_)uk] 
Sent: 06 October 2006 11:14
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] working out where the last() is

Hi,

I have some XML data which contains an areaserved node as follows:

<areaserved>
    <district name="South Hams">
            <town name="Dartmouth">
                    <settlement name="Kingswear"/>
            </town>
            <town name="Totnes"/>
    </district>
    <district name="Torbay"/>
</areaserved>

I am trying to write some XSLT to transform this to a list 
as follows:

Kingswear, Totnes, Torbay

In other words I want to output the name attribute of 
elements that have no children and I want these names to be 
seperated by a comma. I can output the attributes ok but my 
problem is trying to prevent the code putting out a comma at 
the end of the list, given that I don't know whether the end 
of the list will be a settlement, town or district. I have 
tried using last() like this:

<xsl:if test="position() != last()">,&#160;</xsl:if>

but this only works on the context node, so in this case 
Kingswear, Totnes and Torbay are all the last and none of 
them get a comma! 

Any ideas anyone?

Many Thanks
Rob Walpole
(robert(_dot_)walpole(_at_)devon(_dot_)gov(_dot_)uk)





--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>