RE: [xsl] Returning a string from a function

Subject: RE: [xsl] Returning a string from a function
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Wed, 9 Jun 2004 23:47:31 +0100
If the select expression in xsl:value-of is returning a sequence of strings,
and you want the strings joined together without a separator, you can
specify separator="" on the xsl:value-of instruction (the default value is
separator=" ", a single space).

However, If you want to return a string from the function, rather than a
text node, I would suggest using xsl:sequence rather than xsl:value-of. You
will then need to use string-join() to merge the strings in the sequence.

To show you an alternative coding style, I would be inclined to write this
function as:

<xsl:function name="local:logicalName" as="xs:string">
  <xsl:param name="name" as="xs:string"/>
  <xsl:variable name="words" as="element()*"
                select="$locals/lookup/words/word"/>
  <xsl:variable name="mapped-words" as="xs:string*" select="
    for $substring in tokenize($name, '_') return
      if ($words[@physical=$substring])
      then if ($words[@physical=$substring and @logical eq '*'])
           then $substring
           else $words[@physical=$substring]/@logical
      else if ($words[@shortPhysical=$substring])
           then if ($words[@shortPhysical=$substring and @logical='*'])
                then $substring
                else $words[@shortPhysical=$substring]/@logical
           else concat(substring(., 1, 1), lower-case(substring(., 2)))
     "/>
  <xsl:sequence select="string-join($mapped-words, '')"/>
</xsl:function> 

Not tested!

> -----Original Message-----
> From: Barry Lay [mailto:blay@xxxxxxxxxxxxxxxxxxx] 
> Sent: 09 June 2004 21:42
> To: xsl-list
> Subject: [xsl] Returning a string from a function
> 
> I have the following in a stylesheet:
> 
> <xsl:function name="local:logicalName">
>   <xsl:param name="name"/>
>  
>   <xsl:analyze-string select="$name" regex="_">
>     <xsl:non-matching-substring>
>     <xsl:variable name="substring" select="."/>
>       <xsl:choose>
>         <xsl:when 
> test="$locals/lookup/words/word[@physical=$substring]">
>           <xsl:choose>
>             <xsl:when 
> test="$locals/lookup/words/word[@physical=$substring and 
> @logical='*']">
>               <xsl:value-of select="$substring"/>
>             </xsl:when>
>             <xsl:otherwise>
>               <xsl:value-of 
> select="$locals/lookup/words/word[@physical=$substring]/@logical"/>
>             </xsl:otherwise>
>           </xsl:choose>
>         </xsl:when>
>         <xsl:when 
> test="$locals/lookup/words/word[@shortPhysical=$substring]">
>           <xsl:choose>
>             <xsl:when 
> test="$locals/lookup/words/word[@shortPhysical=$substring and 
> @logical='*']">
>               <xsl:value-of select="$substring"/>
>             </xsl:when>
>             <xsl:otherwise>
>               <xsl:value-of 
> select="$locals/lookup/words/word[@shortPhysical=$substring]/@
logical"/>
>             </xsl:otherwise>
>           </xsl:choose>
>         </xsl:when>
>         <xsl:otherwise>
>           <xsl:value-of select="concat(substring(., 1, 1), 
> lower-case(substring(., 2)))"/>
>         </xsl:otherwise>
>       </xsl:choose>
>     </xsl:non-matching-substring>
>   </xsl:analyze-string>
> </xsl:function>
> 
> The $locals document contains reserved words that have special 
> conversion rules, like this:
> 
> <?xml version="1.0"?>
> <lookup>
>     <words>
>         <word physical="ACCT" shortPhysical="*" comment="*" 
> logical="Account"/>
> ...
>         <word physical="ZIP" shortPhysical="*" comment="*" 
> logical="*"/>
>     </words>
> </lookup>
> 
> What I would like it to do is convert
> 
> ACCOUNT_TO_GROUP
> 
> into
> 
> AccountToGroup
> 
> via <xsl:value-of select="local:logicalName(@name)"/> but 
> instead it is 
> putting spaces between the words.  I understand that xsl:function is 
> defined to return a sequence which is what I guess is going 
> on here.  I 
> can use string-join to squish them back together, but is 
> there a way to 
> output the desired result from the function directly?
> 
> Thanks,
> Barry
> 
> --+------------------------------------------------------------------
> 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@xxxxxxxxxxxxxxxxxxxxxx>
> --+--
> 
> 



Current Thread