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 functionTo: NULL Date: 7/10/2009 10:52:00 AM On Jul 9, 6:57=A0pm, cms...@acm.org (C. M. Sperberg-McQueen) wrote:
> johkar<nosendj...@msn.com> writes:
> > Thanks for the reply, I am still not quite getting why the rateCode is
> > getting transformed into FCTR2 without the underscore. =A0You 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.
>
> As I wrote in my previous note,
>
> =A0 =A0 The first argument is scanned character by character, and each
> =A0 =A0 character is tested to see if it appears anywhere in the second
> =A0 =A0 argument. =A0If it does not appear in the second argument, it's
> =A0 =A0 copied to the output string without change. =A0If it does appear =
in
> =A0 =A0 the second argument at position N, then it's replaced in the
> =A0 =A0 output by the character at position N of the third argument. =A0I=
f
> =A0 =A0 the third argument is less than N characters long, the character
> =A0 =A0 is omitted from the output.
>
> That was my attempt to explain why I would expect that. =A0If it
> didn't help, let me try again.
>
> The inner call to translate() has three arguments:
>
> =A0 (1) the string "FCTR[ =A0 ]2"
> =A0 (2) the string "[ ]"
> =A0 (3) the string "_"
>
> The function constructs an output string by walking through the input
> string character by character and possibly adding something to the
> output string.
>
> Character 1: =A0"F".
>
> =A0 Look for an "F" in argument 2. =A0Find none.
> =A0 Place "F" in the output string, which is now "F".
>
> Character 2: =A0"C".
>
> =A0 Look for a "C" in argument 2. =A0Find none.
> =A0 Place "C" in the output string, which is now "FC".
>
> Character 3: =A0"T".
>
> =A0 Look for a "T" in argument 2. =A0Find none.
> =A0 Place "T" in the output string, which is now "FCT".
>
> Character 4: =A0"R".
>
> =A0 Look for an "R" in argument 2. =A0Find none.
> =A0 Place "R" in the output string, which is now "FCTR".
>
> Character 5: =A0"[".
>
> =A0 Look for an occurrence of "[" in argument 2. =A0Find one
> =A0 at position 1. =A0Look up the character in position 1
> =A0 of argument 3; find "_". =A0Add that character ("_")
> =A0 to the output string, which is now "FCTR_".
>
> Character 6: =A0" ".
>
> =A0 Look for an occurrence of " " in argument 2. =A0Find one
> =A0 at position 2. =A0Look up the character in position 2
> =A0 of argument 3; discover that there isn't one (argument
> =A0 3 is one character long). =A0So add nothing to the
> =A0 output string; it remains "FCTR_".
>
> Character 7: =A0" ".
>
> =A0 Look for an occurrence of " " in argument 2. =A0Find one
> =A0 at position 2. =A0Look up the character in position 2
> =A0 of argument 3; discover that there isn't one (argument
> =A0 3 is one character long). =A0So add nothing to the
> =A0 output string; it remains "FCTR_".
>
> Character 8: =A0" ".
>
> =A0 Look for an occurrence of " " in argument 2. =A0Find one
> =A0 at position 2. =A0Look up the character in position 2
> =A0 of argument 3; discover that there isn't one (argument
> =A0 3 is one character long). =A0So add nothing to the
> =A0 output string; it remains "FCTR_".
>
> Character 9: =A0"]".
>
> =A0 Look for an occurrence of "]" in argument 2. =A0Find one
> =A0 at position 3. =A0Look up the character in position 3
> =A0 of argument 3; discover that there isn't one (argument
> =A0 3 is one character long). =A0So add nothing to the
> =A0 output string; it remains "FCTR_".
>
> Character 10: =A0"2".
>
> =A0 Look for an occurrence of "2" in argument 2. =A0Find none.
> =A0 So add "2" to the output string, which is now "FCTR_2".
>
> End of argument 1: return the output string, now "FCTR_2".
>
> The details for characters 5 through 9 should make clear that what
> translate() may be expected to do with second and third arguments of
> "[ ]" and "_" is to replace "[" with "_" and delete " " and "]".
>
> > <rateCode>FCTR[ =A0 ]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}">
> > =A0 =A0<xsl:value-of select=3D"rateTag/text()"/>
> > </xsl:element>
>
> Interesting. =A0When I cut and paste your code fragment into a
> stylesheet and run it on your input, both xsltproc and Saxon give me
>
> =A0<FCTR_2>0.04200</FCTR_2>
>
> not
>
> =A0<FCTR2>0.04200</FCTR2>
>
> Similar tests show that the XSLT processors in Safari, Opera,
> and Firefox all produce "FCTR_2" not "FCTR2" from the input you
> describe.
>
> Two questions: =A0
>
> (1) Are you sure that the code you quote is actually the code that is
> producing the output you quote? =A0Try replacing
>
> =A0 <xsl:variable name=3D"tag"
> =A0 =A0select=3D"translate(translate(rateCode/text(),'[ ]','_'),'.','__')=
"/>
>
> with
>
> =A0 <xsl:variable name=3D"tag" select=3D"hi_mom"/>
>
> to see if your output is still <FCTR2>0.04200</FCTR2> or changes to
> <hi_mom>0.04200</hi_mom>.
>
> (2) If your output does change, indicating that the code you
> quote really is the code doing the work, then I become curious:
> What XSLT processor are you using?
>
> HTH
>
> Michael Sperberg-McQueen
>
> --
> ****************************************************************
> * C. M. Sperberg-McQueen, Black Mesa Technologies LLC
> *http://www.blackmesatech.com
> *http://cmsmcq.com/mib
> *http://balisage.net
> ****************************************************************
Ok, a light bulb has finally gone off thanks to your detailed
explanation. I appreciate the effort in your reply. I was confused a
bit regarding which of my tests produced the FCTR2 without the
underscore. XMLSpy's default processor, Microsoft's MSXML and Xalan
all produce FCTR_2 using the example given. I was doing some testing
with multiple spaces using the same argument 2 and it produced FCTR2.
Sorry for the confusion, but I understand the how and why now. Thanks.
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
