RE: [xsl] Selecting last text() from fragment of unknown depth?

Subject: RE: [xsl] Selecting last text() from fragment of unknown depth?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 5 Feb 2005 16:53:01 -0000
> With this template
> 
> <xsl:template 
> match="submission.excerpt//node()/@*|submission.excerpt//node()">
> <xsl:copy><xsl:apply-templates select="./@*|./node()" /></xsl:copy>
> </xsl:template>
> 
> have I made it impossible to also transform (inline) the last 
> occurring 
> textnode beneath submission.excerpt/node()?

Nothing is impossible, but selecting whether you're the last text node in a
subtree is a little tricky in 1.0.  I often find it useful to work back from
a 2.0 solution, which you could do by adding the template rule:

<xsl:template match="text()[. is
(ancestor::submission.excerpt//text())[last()]]" priority="5"
  <xsl:value-of select="."/>...
</xsl:template>

You haven't got the "is" operator in 1.0, but you can replace (A is B) by
(generate-id(A) = generate-id(B)).

This solution could be rather expensive if the number of text nodes in a
submission.excerpt is large.

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

> 
> So far all my recursion attempts (looking for 
> node()[last()][self::text()] 
> at each level) have yielded 3 variations: *only* the last 
> text(), everything 
> *except* the last text(), or the whole thing and then the last text() 
> appended again.
> 
> <xsl:choose>
> <xsl:when test="./node()[last()][self::text()]">
> 	<xsl:copy>
> 		<xsl:apply-templates 
> select="./@*|./node()[position() != last()]" />
> 		<xsl:value-of select="./node()[last()]" />...
> 	</xsl:copy>
> </xsl:when>
> <xsl:otherwise>
> 	<!-- look again next level, etc -->
> </xsl:otherwise>
> </xsl:choose>
> 
> It's meant to take an XHTML string (truncated from db then 
> made well-formed 
> again by PHP5's loadHTML method) and replace the end of the 
> last text() -- 
> after the last occuring space character -- with an ellipsis, 
> which I've now 
> heard 3x is 'impossible' in XSLT 1.0 but it's not official 
> till I've heard 
> it here.
> 
> Undying gratitude,
> -Adam.
> 
> _________________________________________________________________
> Take advantage of powerful junk e-mail filters built on 
> patented MicrosoftR 
> SmartScreen Technology. 
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&;
> DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines 
>   Start enjoying all the benefits of MSNR Premium right now 
> and get the 
> first two months FREE*.

Current Thread