RE: [xsl] [XSL] Calculating Length of String Variables

Subject: RE: [xsl] [XSL] Calculating Length of String Variables
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 13 Sep 2007 08:29:51 +0100
Firstly, don't use this construction:

<xsl:variable name="image_src">
       <xsl:value-of select="./@url"/>
</xsl:variable>

Instead, write:

<xsl:variable name="image_src" select="./@url"/>

That's nothing to do with this problem but it's a bad and very common coding
habit that you should try to break.

If the variable is a global variable, as you seem to be suggesting, then "."
is the root (document) node, and document nodes don't have any attributes.
If your XML only contains the one element you showed, then you should do
select="/graphic/@url".

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

> -----Original Message-----
> From: Alice Wei [mailto:ajwei@xxxxxxxxxxx] 
> Sent: 13 September 2007 03:38
> To: xsl-list
> Subject: [xsl] [XSL] Calculating Length of String Variables
> 
> Hi, 
> 
>  I am trying to create different sets of output based on the 
> length of the "string" of my variable. Below are the lines I 
> used for calculation:
> 
>  <xsl:variable name="image_src">
>         <xsl:value-of select="./@url"/>
>     </xsl:variable>
> 
>  <xsl:template match="graphic">
>         <xsl:value-of select="string-length($image_src)"/>
> </xsl:template>
> 
> 
> If I have XML like: <graphic url="0125"/>. the result is 
> supposed to come out with 4. And, likewise, <graphic 
> url="123456"/> should come out with 6. However, when I run 
> the XSLT, it only gives me "0". Is there something I have done wrong?
> 
> Thanks to those who may help.
> 
> Alice

Current Thread