xsl-list
[Top] [All Lists]

RE: [xsl] param vs context for passing arguments

2009-12-16 18:09:17
 

This is simple template to replace hyphen with underscore in 'name' 
attribute of some element;

<xsl:template name="ReplaceHyphenWithUnderscore">
   <xsl:analyze-string select="@name" regex="-">
     <xsl:matching-substring>
       <xsl:text>_</xsl:text>
     </xsl:matching-substring>
     <xsl:non-matching-substring>
       <xsl:value-of select="."/>
     </xsl:non-matching-substring>
   </xsl:analyze-string>
</xsl:template>

Your code will be most versatile (reusable) if you write it as a function to
operate on any string.

<xsl:function name="f:ReplaceHyphenWithUnderscore" as="xs:string">
      <xsl:param name="input" as="xs:string"/>
   <xsl:analyze-string select="$input" regex="-">
     <xsl:matching-substring>
       <xsl:text>_</xsl:text>
     </xsl:matching-substring>
     <xsl:non-matching-substring>
       <xsl:value-of select="."/>
     </xsl:non-matching-substring>
   </xsl:analyze-string>
</xsl:template>

Then you can write, for example

<xsl:template match="xsd:element[<some condition here>]">
   <xsl:value-of name="f:ReplaceHyphenWithUnderscore(@name)"/>
   (...)
</xsl:template>


Having said that, this particular operation can be achieved by a single call
of translate():

<xsl:function name="f:ReplaceHyphenWithUnderscore" as="xs:string">
      <xsl:param name="input" as="xs:string"/>
      <xsl:sequence select="translate($in, '-', '_')"/>
</xsl:template>

Regards,

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


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