Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt 1.0, string-length function

Tags:

xslt

xpath

I saw string-length on this website http://www.xsltfunctions.com/xsl/fn_string-length.html I think it's the xslt 2.0 syntax.

though, in xslt 1.0, i tried this:

<xsl:template match="/">
    <xsl:value-of select="string-length(())" /> 
</xsl:template>

and i got an error. is there any function that i can use to convert () and pass it to string-length so that I can get 0?

like image 477
green_tea2009 Avatar asked Sep 29 '09 17:09

green_tea2009


2 Answers

The XPath 1.0 string-length() takes a single string argument. Anything that is not a string will be converted to a string for you. An empty set of parentheses can't be converted, and there are no sequences in 1.0. To get 0, you can feed the empty string:

<xsl:template match="/">
  <xsl:value-of select="string-length('')" /> 
</xsl:template>

to get something meaningful, you can feed a node-set, for example:

<xsl:template match="/">
  <xsl:value-of select="string-length(some/xpath)" /> 
</xsl:template>

The first node in the node-set would be converted to a string and passed to the function.

like image 87
Tomalak Avatar answered Sep 16 '22 13:09

Tomalak


A little more info: If you don't pass any argument to string-length() it will evaluate the current context node.

Note that the xpath passed as argument is relative to the current context in the xpath expression

like image 26
neves Avatar answered Sep 18 '22 13:09

neves