Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Help deciphering use of square brackets within translate function >Thread Next - Re: Help deciphering use of square brackets within translate function Re: Help deciphering use of square brackets within translate functionTo: NULL Date: 7/9/2009 12:04:00 PM On Jul 3, 11:27=A0am, cms...@acm.org (C. M. Sperberg-McQueen) wrote:
> johkar<nosendj...@msn.com> writes:
> > To me, the inner translate function below would find any instance of
> > square brackets with a single space separating them and replace it
> > with a single underscore but that doesn't seem to be what is
> > happening. =A0Do the square brackets have some sort of
> > significance...like a regular expression? =A0I didn't think so since
> > they were quoted. =A0I think the original developer's intent was to
> > replace spaces with underscores so that the element name syntax would
> > be valid XML.
>
> The second argument to translate() is interpreted as a set of
> characters, not as an expression. =A0The first argument is scanned
> character by character, and each character is tested to see if it
> appears anywhere in the second argument. =A0If it does not appear in the
> second argument, it's copied to the output string without change. =A0If
> it does appear in the second argument at position N, then it's
> replaced in the output by the character at position N of the third
> argument. =A0If the third argument is less than N characters long,
> the character is omitted from the output.
>
> This behavior will be familiar to some people from analogous
> functions and operations in Snobol, Spitbol, Rexx, and
> IBM 360 assembler, as well as (I think) some other languages.
>
> > However if the XML contains:
>
> > <rate>FCTR[ =A0 ]2</rate> =A0 it transforms into =A0<FCTR2>0.04200</FCT=
R2>
> > without any underscore at all.
>
> > <xsl:variable name=3D"tag" select=3D"translate(translate(rateCode/text
> > (),'[ ]','_'),'.','__')"/>
> > <xsl:element name=3D"{$tag}">
> > =A0 =A0<xsl:value-of select=3D"rateTag/text()"/>
> > </xsl:element>
>
> > Any insight into this would be appreciated.
>
> The inner call, to
>
> =A0 =A0 translate(rateCode/text(),'[ ]','_')
>
> should translate left square bracket to underscore and omit any
> blanks or right square brackets. =A0In your example, the element is
> named rate, not rateCode, which means we cannot tell what the
> first argument of the inner call to translate() is. =A0If the
> XML actually contains <rateCode>FCTR[ =A0 ]2</rateCode>, then I
> would expect the call to produce "FCTR_2". =A0The outer call is then
>
> =A0 =A0 translate("FCTR_2",'.','__')
>
> and since no full stops appear in the first argument, the result
> would be FCTR_2. =A0This is what you say you expected, but the
> reasoning is rather different.
>
> If there is a rateCode element in the vicinity, then the result
> will depend on its content.
>
> Are you sure you transcribed both the input and the XSLT
> correctly here?
>
> hth
>
> --
> ****************************************************************
> * C. M. Sperberg-McQueen, Black Mesa Technologies LLC
> *http://www.blackmesatech.com
> *http://cmsmcq.com/mib
> *http://balisage.net
> ****************************************************************
Thanks for the reply, I am still not quite getting why the rateCode is
getting transformed into FCTR2 without the underscore. You stated
that rateCode "should translate left square bracket to underscore and
omit any blanks or right square brackets"...why would you expect
that...just trying to understand.
<rateCode>FCTR[ ]2</rateCode>
<rateTag>0.04200</rateTag>
transforms into
<FCTR2>0.04200</FCTR2> without any underscore at all.
<xsl:variable name=3D"tag" select=3D"translate(translate(rateCode/text
(),'[ ]','_'),'.','__')"/>
<xsl:element name=3D"{$tag}">
<xsl:value-of select=3D"rateTag/text()"/>
</xsl:element>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
