Re: [xsl] Translating "("

Subject: Re: [xsl] Translating "("
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 15 Nov 2004 12:36:03 GMT
  I need translate special characters like "(", ")" etc in <ln:term>.

  <xsl:variable name="val" select="translate(normalize-space(ln:term),
  '&#39;&#160;/&#40;&#41;,&#34;""', '_')" />

  Unfortunately Saxon throws an exception at this line, because of
  resolving the character entities...


There's nothing special about ( in strings, you could just use ( to
denote those. and &#39; are not entity references but character
references (which are expanded at a different time)

You didn't show the error message you got, although it's unlikely to
have been from saxon, your quoted line is not well formed XML (It has a
two literal " characters inside a " delimited attribute value) so should
have been rejected by the XML parser before XSLT processing starts.

 <xsl:variable name="val" select="translate(normalize-space(ln:term),
                                 ^
  '&#39;&#160;/&#40;&#41;,&#34;""', '_')" />
                               ^^       ^

Getting ' and " into an Xpath string is a FAQ )and the faq for this list
will have an entry for it but you can not have a string literal that
contains both ' and " You appear to want to have the Xpath string

 '<nbsp>/(),"

You can have ' in a "-delimited string and " in a 'delimited string but
you can't have both in the same literal so you need to concat them
together eg

concat("'&#160;/()",'"')

Your translate function just had a string of length 1 so that's goint to
replace the first character  ' by _ and discard the rest, is that what
you want? assuming so:

You need

translate(normalize-space(ln:term),
   concat("'&#160;/()",'"')
   '_')

now to get that expression into a select attrbute. As far as XML is
concerned it's all just an opaque string, that has three " and five '
so if you are using " to delimit the attrinute you need to &quot; the
occurrences of " in the attribute value

select="translate(normalize-space(ln:term),
   concat(&quot;'&#160;/()&quot;,'&quot;')
   '_')"

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread