Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 21st, 2005, 03:13 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL Translate Problem

Hello.
Any xml nodes that I have containing a dollar sign followed by a one, show up after the xsl as just the dollar sign. For example:

$100 shows up as $00

I can't make any changes to the xml file that is being generated by a content management program. I just have to deal with what it spits out.

I am using"
<xsl:variable name="title" select="translate(PRTitle,'$','#36 ;')" /> and all is fine. The problem is that I also need to translate another character:
<xsl:variable name="title2" select="translate($title,'&amp;#174;','®')" />

I can't get both to translate, only one or the other.

Any ideas on how to do this would be greatly appreciated.
Thanks.
Reply With Quote
  #2 (permalink)  
Old March 21st, 2005, 03:46 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you want to represent the single character whose Unicode code point is 174, you can write it as &_#174; (omitting the underscore). If you write &_amp;#174; (omitting the underscore) you are representing a string of 6 characters - ampersand, hash, one, seven, four, semicolon. The ampersand will then be replaced by a registered sign, and all occurrences of the other five characters will be deleted. Note that translate() replaces each one of the characters in the second argument by the corresponding character in the third argument - it doesn't replace the string as a whole.

Replacing the Unicode character whose codepoint is 174 by a registered-sign is a no-op, because 174 is the codepoint for this symbol.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old March 21st, 2005, 04:01 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have been given xml that has '&amp;#174;' in it and am able to replace it with '®' with no problem. If I try doing as you suggest, I get '&reg;' in the page output. Not what I want.

The problem is that I also need to replace '$' with '#36;'. The first change works, but the second does not. If I reverse the two, the first change takes effect and the second is ignored again even though they are switched.

I just need to know how to get both to change. Sorry for the confusion.

Thanks.
Reply With Quote
  #4 (permalink)  
Old March 21st, 2005, 04:30 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, sorry.
I see what you are saying now.
I'll give it a try.
Thanks.
Reply With Quote
  #5 (permalink)  
Old March 21st, 2005, 05:16 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If translate($title,'&amp;#174;','®') changes a character written in your source as &amp;#174; then you are using a very buggy processor, and I have no idea what it will do with translate(PRTitle,'$','#36 ;')



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old March 21st, 2005, 05:40 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, lots of craziness happening.

1. It seems that translate(PRTitle,'&amp;#174;','®') is causing the problem with $100 showing up as $00, but shows the ® with no problem. If I take out the translate, $100 shows up fine, but I get &amp;#174 on the page again.

2. If I do: translate(PRTitle,'&_#174;','®')(without the "_") I get &reg;

Ideally, what would I want to have in the xml itself for $ and ® to ensure that it will translate properly on the page? Right now if I have ® in my xml, I get an error.

The end result I am looking for is to have ® and $100 show up correctly. It would take some work, but I could get the people creating the xml to change what they have in the xml if it will solve the problem.

Thanks again.
Reply With Quote
  #7 (permalink)  
Old March 21st, 2005, 06:16 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think you are misunderstanding two things.

(a) the way translate works.

translate($in, 'ABCDE', 'VWXYZ') changes every A in $in to a V, every B to a W, every C to an X, every D to a Y, and every E to a Z. If the second string is longer, e.g. translate($in, 'ABCDE', 'V') then A is changed to V, and B,C,D, and E are removed from the string. This is why the "1" is disappearing from your string.

(b) the way character references work. A character reference such as '&_#174;' (witout the underscore) is translated by the XML parser into an ordinary character. As far as the XSLT and XPath processors are concerned, it's a single character. So translate($in, '&_#174;', 'R') means replace each occurrence of the character ® by R. That's because &_#174; (without the underscore) is nothing more than a convenient way of entering the ® character from your keyboard.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #8 (permalink)  
Old March 21st, 2005, 06:22 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You asked anohter question:

Right now if I have ® in my xml, I get an error.

That suggests there is a third thing you're confused about, namely character encodings.

An XML document is a sequence of bytes. The bytes represent characters. There are many ways of encoding the same sequence of characters as a sequence of bytes: these are called encodings. An XML document declares the encoding it is using at the start of the file in an XML declaration, e.g. <?xml version="1.0" encoding="iso-8859-1"?>. The default encoding is UTF-8. Whichever encoding you declare in the file (explicitly or implicitly), it must match the encoding used by your text editor when you entered the text. Many modern text editors support UTF-8 encoding, but it's not usually the default. If your editor is encoding ® using ISO-8859-1, then you must specify encoding="iso-8859-1" in your XML declaration, or you will either get an error from the XML parser, or it will be misinterpreted as a different character.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #9 (permalink)  
Old March 22nd, 2005, 11:26 AM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, I think I understand now.
Thank you for taking the time to explain.
I've now realised that the problem has nothing to do with the xsl and can see that the xml document is having problems displaying the ® character when I view it in a browser.

How do I set the encoding? I have tried setting it at the top of the document in the <?xml version="1.0" encoding="iso-8859-1"?> statement, but this has no effect. Do I need to somehow do this in the program that is creating the xml doc? Is there a specific encoding that I need to use? I have tried utf-8, utf-16, windows-1252 and iso-8859-1.

Thanks again for all of the help. I really appreciate it.
Reply With Quote
  #10 (permalink)  
Old March 22nd, 2005, 12:20 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is now probably a quesion of browser settings. You don't say which browser. I'm no expert on browser settings - at present I can't even get Internet Explorer to display XML, it always launches my XML editor... But a file containing ISO-8859-1 characters, with encoding="iso-8859-1" in the XML declaration, displays fine in Firefox.



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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you translate? ms_sheila CSS Cascading Style Sheets 2 May 13th, 2007 08:54 PM
How can i translate this VB code into C#? Dreamfly ASP.NET 2.0 Basics 2 April 13th, 2007 12:53 AM
translate gantait XSLT 1 February 27th, 2007 04:15 AM
How do you translate an apostrophe? IronStar XSLT 2 November 13th, 2006 12:35 PM
how to translate this coding sime_tyres Pro VB 6 0 January 17th, 2006 09:32 PM



All times are GMT -4. The time now is 01:40 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.