3

i have this function in xsl i took from this post

to replace "cr" with "line-break"

this is how i call it:

<xsl:variable name="breakText">
     <xsl:call-template name="insertBreaks"> 
          <xsl:with-param name="subject" select="PublicProfile/AboutMe"/> 
         </xsl:call-template>
    </xsl:variable>

im doing like an article link to "read more" on text click the first div of the short start of the article (550 chars of the long article) display the text like i didnt use the "insertBreaks" function

<xsl:copy-of select="substring($breakText,0,550)"/>

but this line of the long description is working fine:

<xsl:copy-of select="$breakText"/>

where i was wrong?

Community
  • 1
  • 1
roy.d
  • 1,030
  • 2
  • 16
  • 33

2 Answers2

2

You want to use xsl:value-of not xsl:copy-of. xsl:copy-of returns the elements and all their values of the selected tag. xsl:value-of returns the text associated with the selected tag. Since you're trying to get a substring, you want the text.

yamen
  • 15,390
  • 3
  • 42
  • 52
  • `$breakText` likely points to an element. You can do `copy-of` on an element. You can't do `substring` on an element though, only its text, and you certainly can't do `copy-of` on the resulting string. – yamen Apr 16 '12 at 09:56
  • how can i show the text after i replaced what i want in both cases substring and full? – roy.d Apr 16 '12 at 09:58
0

The solution used referred to in the question happens to be mine.

It replaces a newline character not with "line-break" but with a <br /> element. An element is a node -- not a string.

Thus, the $breakText as defined in this question is a temporary tree (in XSLT 2.0) or an RTF (Result Tree Fragment) in XSLT 1.0 -- not a string.

The problem is that (regardless of an accompanying minor error of using zero as index):

substring($breakText,0,550)

is a string (calculated from the string value of $breakText) and as such cannot include any <br/> element or any node at all

Remember: The string value of an element or of a tree is the concatenation of the values of all of its text-node descendents.

On the other side:

<xsl:copy-of select="$breakText"/> 

copies the temporary tree (in XSLT 2.0) or produces the serialization of the RTF (in XSLT 1.0).

To put it more simply, xsl:copy-of preserves structure, string() or any conversion to string produces just a string -- and a string doesn't have structure (all nodes are gone).

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • @roy.d: Please, edit the question and provide a complete (but as small as possible) example of an XML document, an XSLT transformation and the result you get, so that people could repro the problem -- then many people would be able to explain exactly what causes the unwanted output and provide a correct solution. – Dimitre Novatchev Apr 16 '12 at 13:16
  • i edit the problame i have, the xml is not the issue also. i convert \r\n to
    using function like i wrote above.
    – roy.d Apr 17 '12 at 05:18
  • @roy.d: I edited my answer accordingly, explaining the difference between *copying* a node-set and converting it just to string. And this is the cause of your problem. – Dimitre Novatchev Apr 17 '12 at 12:31
  • to solve it i call the function with parametes of substring text – roy.d Apr 19 '12 at 06:41
  • @roy.d: I am glad my answer was useful. – Dimitre Novatchev Apr 19 '12 at 11:49