views:

40

answers:

2

I've scoured the internet to discover this method of finding the number of lines in an xml element.:

<xsl:if test="string-length(@Example) - string-length(translate(@Example, '&#xa;', '')) &lt; 10"> 

In the example above, @Example is the element for which the number of lines is counted. I didn't like this code, however, because it automatically gets turn into this:

    <xsl:if test="string-length(@Example) - string-length(translate(@Example, ' 
', '')) &lt; 10">

You see, the code &#xa; gets turned into a literal blank line (which it represents but I don't want it to be a blank line). That above seems like bad coding style to me(if it isn't, please tell me), so I want an alternate way for finding the number of lines in @Example. Thank you.

A: 

This style is probably closer to your preferences:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

 <xsl:variable name="vNL" select="'&#xA;'"/>

 <xsl:template match="t">
  <xsl:value-of select=
    "string-length(@Example)
    -
     string-length(translate(@Example, $vNL, ''))"/>
 </xsl:template>
</xsl:stylesheet>

The NL constant is kept in a variable and referencing this variable does not cause an editor to display a new line.

Even the $vNL declaration can be cured of this problem -- provided this is made into a global <xsl:param> and the value is provided by the external invoker of the transformation.

Dimitre Novatchev
@Dimitre: Yes. But as I answer previously, only new-line **character reference** in the input source will be keep into attribute value after normalization.
Alejandro
@Alejandro: Sorry, I completely lost you. Is there any problem?
Dimitre Novatchev
A: 

Just for clarification of my previous answer ( http://stackoverflow.com/questions/3366181/sharepoint-designer-keeps-turning-xa-within-source-code-into-literal-new-line )

Running this stylesheet (same as Dimitre):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="text"/>

 <xsl:variable name="vNL" select="'&#xA;'"/>

 <xsl:template match="xsl:value-of">
  <xsl:value-of select=
    "string-length(@select)
    -
     string-length(translate(@select, $vNL, ''))"/>
 </xsl:template>
</xsl:stylesheet>

With itself as input, output:

0

It only works with input like this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="text"/>

 <xsl:variable name="vNL" select="'&#xA;'"/>

 <xsl:template match="xsl:value-of">
  <xsl:value-of select=
    "string-length(@select)&#xA;
    -&#xA;
     string-length(translate(@select, $vNL, ''))"/>
 </xsl:template>
</xsl:stylesheet>

Output:

2
Alejandro