xsl-list
[Top] [All Lists]

Re: [xsl] function returning string with string-join()

2010-08-06 05:46:53
Hi Michael,

On 6 August 2010 09:59, Michael Müller-Hillebrand 
<mmh(_at_)cap-studio(_dot_)de> wrote:
Hello experts,

I find myself building functions like this (no real code!) to return a string:

<xsl:function name="my:filename" as="xs:string">
 <xsl:param name="input" as="xs:integer" />
 <xsl:variable name="strings">
   <xsl:choose>
     <xsl:when test="$input eq 1">
       <xsl:value-of select="'NO1'" />
     </xsl:when>
     <xsl:when test="$input eq 2">
       <xsl:value-of select="'NO2'" />
     </xsl:when>
     <xsl:otherwise>
       <xsl:message select="'otherwise?'" />
       <xsl:value-of select="'NOx'" />
     </xsl:otherwise>
   </xsl:choose>
   <xsl:value-of select="'.html'" />
 </xsl:variable>
 <xsl:value-of select="string-join($strings, '')" />
</xsl:function>

In a variable this builds a sequence of strings using individual xsl:value-of 
and in the end they are joined. Is this the way to do it if I cannot concat() 
everything in a single, large xsl:value-of?

I have a feeling that it might be simpler?

When returning atomics (such as xs:string) you should use xs:sequence
and not xsl:value-of, as value-of will create a text node that then
gets "atomized" to an atomic.

The same goes for the variable, when its contents will be treated as
an atomic, you should use the "as" attribute to type it as an atomic,
otherwise a document node with a single text node child is created
which then has to be navigated to get the string value:

    <xsl:function name="my:filename" as="xs:string">
        <xsl:param name="input" as="xs:integer" />
        <xsl:variable name="strings" as="xs:string">
            <xsl:choose>
                <xsl:when test="$input eq 1">
                    <xsl:sequence  select="'NO1'" />
                </xsl:when>
                <xsl:when test="$input eq 2">
                    <xsl:sequence  select="'NO2'" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message select="'otherwise?'" />
                    <xsl:sequence  select="'NOx'" />
                </xsl:otherwise>
            </xsl:choose>
       </xsl:variable>
       <xsl:sequence select="concat($strings, '.html')" />
    </xsl:function>

If you didnt have the xsl:message call you could do:

    <xsl:function name="my:filename" as="xs:string">
        <xsl:param name="input" as="xs:integer" />
        <xsl:sequence select="concat(if ($input = (1,2)) then ('NO1',
'NO2')[$input] else 'NOx', '.html')"/>
    </xsl:function>



-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

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