Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2007, 02:19 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Translating symbols to their unicode values

Hi,

I am trying to translate all my symbols back to their Unicode values in my XML using XSLT.

All I mean is that I want to translate all my © into its corresponding unicode values i.e., #x000A9;. Below is my XSLT snippet:

<xsl:template name="escape-copy">
 <xsl:param name="string"/>
<xsl:variable name="copy" select="©" />
<xsl:choose>
 <xsl:when test='contains($string, ©)'>
  <xsl:value-of select="substring-before($string,©)"/>
    <xsl:text>#x000A9;</xsl:text>
    <xsl:call-template name="escape-copy">
     <xsl:with-param name="string" select="substring-after($string, ©)" />
    </xsl:call-template>
 </xsl:when>
 <xsl:otherwise>
  <xsl:value-of select="$string" />
 </xsl:otherwise>
</xsl:choose>
</xsl:template>

As soon as try the above code I get an error saying:
"token not recognized".

I understand that may be XSLT processor is not able to read the literal ©, but is there anyway this can done in XSLT. I too have doubt that my code also have errors in it.

Any suggestions will be highly appreciated.

Pankaj


__________________
Pankaj
 
Old September 5th, 2007, 03:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
<xsl:variable name="copy" select="©" />
should be
Code:
<xsl:variable name="copy" select="'©'" />
The copyright symbol should be in single quotes, because it is a string literal, not a element name. This same error is repeated with the contains, substring-before and substring-after functions as well.

This is of course assuming that your xslt stylesheet is utf-8 encoded, otherwise you'll need to replace '©' with '&_#x000A9;' (no underscore).

/- Sam Judson : Wrox Technical Editor -/
 
Old September 5th, 2007, 04:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Unfortunately your terminology is rather confused.

An XSLT stylesheet is an XML document, and in XML, provided you get the character encoding right, © and &_#xa9; (ignore the underscore) are simply different representations of the same character. To an XSLT processor they look exactly the same (the XML parser sorts out the difference. Therefore, XSLT can't be used directly to translate one into the other.

However, if you choose <xsl:output encoding="us-ascii"/>, then the © character cannot be used in the output document, so the serializer is forced to use (something like) &_#xa9; instead.

The errors you are getting for constructs like substring-before($string,©) are simply because you haven't put the character in quotes. You would get a similar syntax error if you wrote substring-before($string,@)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 5th, 2007, 04:50 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I've tried it earlier but nothing seems to be working. I had a doubt whether my code is correct. All I am trying to search copyright symbol (©)in XML and replace it with its corresponding value.

Any guidance on it.

Pankaj

 
Old September 5th, 2007, 05:02 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you're trying to do what I think you're trying to do, and you're using XSLT 2.0 then try looking at the <xsl:character-map> declaration.

Code:
<xsl:output use-character-map="copyright"/>
<xsl:character-map name="copyright">
  <xsl:output-character character="©" string="&amp;#x00A9;" />
</xsl:character-map>

/- Sam Judson : Wrox Technical Editor -/
 
Old September 5th, 2007, 05:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If your code isn't working then your first step should be to show us your code so we can tell you why it isn't working.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 5th, 2007, 05:28 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No Sam, unfortunately I am using 1.0.

Don't we have any function which simply says read the symbol and translate it into unicode value. I know that translate() function takes the three arguments, i don't know whether its a good idea to use it.


Pankaj


 
Old September 5th, 2007, 06:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

As already mentioned, translate() will not work, its a character for character translation, not a string replacement function (unlike the XPath 2.0 replace() function).

I have to ask - what is your reason for wanting to do this in the first place - why do you need the unicode version in your output.

Have you tried the suggestion from Michael?
Code:
<xsl:output encoding="us-ascii"/>
/- Sam Judson : Wrox Technical Editor -/
 
Old September 5th, 2007, 06:39 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. Let me more clear. Below is my code. Can somebody guide me where I am wrong:


 <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="from"/>
    <xsl:param name="to"/>

    <xsl:choose>
      <xsl:when test="contains($text, $from)">

    <xsl:variable name="before" select="substring-before($text, $from)"/>
    <xsl:variable name="after" select="substring-after($text, $from)"/>
    <xsl:variable name="prefix" select="concat($before, $to)"/>

    <xsl:value-of select="$before"/>
    <xsl:value-of select="$to"/>
        <xsl:call-template name="replace-string">
      <xsl:with-param name="text" select="$after"/>
      <xsl:with-param name="from" select="$from"/>
      <xsl:with-param name="to" select="$to"/>
    </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>


 <xsl:template match="/">
     <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="'.'"/>
        <xsl:with-param name="from" select="'©'"/>
        <xsl:with-param name="to" select="&_#x000A9;"/>
    </xsl:call-template>
        <xsl:apply-templates/>
 </xsl:template>



 
Old September 5th, 2007, 06:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

    <xsl:with-param name="from" select="'©'"/>
    <xsl:with-param name="to" select="&_#x000A9;"/>

I assume you meant

<xsl:with-param name="to" select="'&_#x000A9;'"/>

(without the underscore)

As I tried to explain earlier (but you seemed not to be listening), '©' and '&_#x000A9;' are just two different ways of writing the same character. By the time XSLT sees them, they are identical. It's like typing "A" using the CAPS LOCK key rather than the SHIFT key. Your "from" and "to" characters are therefore the same.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
clearing unused symbols and objects nairaby Flash (all versions) 4 August 16th, 2007 02:41 PM
finding symbols keytecstaff Word VBA 1 August 3rd, 2007 11:27 AM
How to get list of Currencies (symbols/Name)? Jell VB.NET 0 June 22nd, 2005 05:48 AM
Symbols and Membership Types rgerald Forum and Wrox.com Feedback 9 August 13th, 2003 10:11 AM
trademark and registered symbols kikoyjr XML 3 July 25th, 2003 03:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.