xsl-list
[Top] [All Lists]

RE: [xsl] length of string from a set of nodes.

2009-03-27 14:02:51

I often tell people to avoid using an xsl:variable with content when they
could use the select attribute.

In fact, if (a) you have an "as" attribute, and (b) the content is
xsl:sequence, then the two constructs are pretty well equivalent. I would
rewrite

<xsl:variable name="slen" as="xs:integer"><xsl:sequence 
  select="string-length($strings)"/></xsl:variable>

as

<xsl:variable name="slen" as="xs:integer" select="string-length($strings)"/>

but the difference is essentially aesthetic.

But value-of should be rewritten:

<xsl:variable name="strings" as="xs:string"><xsl:value-of 
select="answers"/></xsl:variable>

should either be

<xsl:variable name="strings" as="xs:string"><xsl:sequence 
select="answers"/></xsl:variable>

or more simply

<xsl:variable name="strings" as="xs:string" select="answers"/>

That's because xsl:value-of creates a text node, which you don't need to do.

And of course, you could combine the two variables:

<xsl:variable name="slen" as="xs:integer" select="string-length(answers)"/>

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

-----Original Message-----
From: Fred Christian [mailto:fredc(_at_)kbooks(_dot_)com] 
Sent: 27 March 2009 17:46
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] length of string from a set of nodes.

I was able to get this to work, but while reading about it, I 
see all these little tips and recommendations regarding being 
efficient and not creating nodes when you only need strings.

In this code my goal was meet of deciding if the length of 
all the text concatenated together is greater than 30.
    <xsl:variable name="strings" as="xs:string"><xsl:value-of 
select="answers"/></xsl:variable>
    <xsl:variable name="slen" as="xs:integer"><xsl:sequence 
select="string-length($strings)"/></xsl:variable>
    <xsl:for-each select="answers">
        <xsl:choose>
            <xsl:when test="$slen gt 30"> etc...

I tried various combinations of using the <<concat()>> 
function, <<xsl:sequence>>, and <<xsl:value-of>>. This is 
what ended up working.

Is there a "better" way to do this?
Thanks



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