[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: param vs context for passing arguments

Subject: RE: param vs context for passing arguments
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 16 Dec 2009 23:08:50 -0000
RE:  param vs context for passing arguments
> 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 

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.