RE: [xsl] substring-before doesn't copy node

Subject: RE: [xsl] substring-before doesn't copy node
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 12 Jan 2007 10:06:04 -0000
You seem to be confused about the data model. Your title mentions "nodes",
but your message mentions "tags"; you seem to be in a state where you
half-know that the input is represented as a tree but you're still mentally
behaving as if it's all a single string in which elements are represented by
textual markup.

You haven't really said what $test-text is, but my guess is that it's an
element node with three children, namely a text node containing
http://dominio.com/, an element node named b whose content is pippo, and a
text node whose content is /pluto.html. When you apply substring-before() to
an element node, it converts the first argument to a string, which is done
by taking the string-value of the element node. This is the concatenation of
all the text node descendants, namely "http://dominio.com/pippo/pluto.html";.
So you lose the hierarchic structure of the input (or, as you put it, the
"tags").

If you want to process the text while retaining the structure of the tree,
then flattening the structure into a string isn't going to help. You need to
process each text node individually. Of course, some things are going to be
very difficult to achieve, like highlighting the string "com/pip" which
straddles two nodes. Your first task is to specify what you want the output
to be in such cases.

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

> -----Original Message-----
> From: Marco [mailto:spinmar@xxxxxxxxx] 
> Sent: 12 January 2007 09:36
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] substring-before doesn't copy node
> 
> Hi,
> I'm writing a recursive template and I have some problems 
> with subtring-before.
> I'm trying to write a words highlighter.
> In my template if my text contains the word I'm looking for, I write:
> 
> <xsl:variable name="before" 
> select="substring-before($test-text, .)" /> <xsl:variable 
> name="after" select="substring-after($test-text, .)"/> 
> <xsl:variable name="real-before" select="substring($text, 1, 
> string-length($before))"/> <xsl:variable name="real-after" 
> select="substring($text,
> string-length($before) + string-length(.) + 1)"/>
> 
> The problem is that my test-text contains for example 
> http://dominio.com/&lt;b&gt;pippo&lt;/b&gt;/pluto.html and 
> I'm looking for pluto, the before string is 
> http://dominio.com/pippo/. It seems that substring-before 
> deletes my tag <b></b>. I suppose that the problem is in the 
> substring-after too.
> I tried to write:
> <xsl:variable name="before"><xsl:copy-of 
> select="substring-before($test-text, .)" /></xsl:variable> 
> but the problem remains.
> How could I solve my problem?
> Best regards
> Marco

Current Thread