[XSL] How do i get & #160; to pass through?

Collin VanDyck

Usenet Archive
Joined
May 5, 2003
Messages
134
Reaction score
0
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem..

I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation.

Take this source XML document:

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
some space separated text
</sourcexml>

And this stylesheet:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput method="xml" indent="yes"/> <xsl:template match="@*|*|text|processing-instruction"> <xsl:copy> <xsl:apply-templates select="@*|*|text|processing-instruction"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

I am trying to get it to regurgitate the original document, with the 's intact. Instead I am getting bizarre characters (copied from windows CMD window):

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
someáspaceáseparatedátext
</sourcexml>

Here is how I am doing my transform (java code):

SAXSource in = new SAXSource(new InputSource(new
StringReader(this.xmlDocument)));

// build the out result
StringWriter writer = new StringWriter;
StreamResult out = new StreamResult(writer);

// build the transformer
SAXSource stylesheetIn = new SAXSource(new InputSource(new StringReader(this.xslStylesheet))); Transformer transformer = TransformerFactory.newInstance.newTransformer(stylesheetIn);

// transform the string.
transformer.transform(in,out);

// return the transformation result.
return writer.toString;

Any ideas? Any help would be very appreciated. Thanks :)
 

Joona I Palaste

Usenet Archive
Joined
Jun 24, 2003
Messages
2,272
Reaction score
0
Collin VanDyck scribbled the following
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem..
I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation.
Take this source XML document:
<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
some space separated text
</sourcexml>

I'm not sure if this is what you want, but one way to get the literal string " " to appear in the output is to write it as:
"&amp;#160;" in the source code. To get *that* to appear, write it as "&amp;amp;#160;" and so on. Of course I could be trying to solve the wrong problem here.
 

Collin VanDyck

Usenet Archive
Joined
May 5, 2003
Messages
134
Reaction score
0
Hi...

Thanks for the reply, but I tried that, and it produced:

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
some&amp;amp;#160;space&amp;amp;#160;separated&amp;amp;#160;text </sourcexml>

where what I am aiming to get is:

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
some space separated text
</sourcexml>

thanks,
 

Anton Spaans

Usenet Archive
Joined
Oct 27, 2003
Messages
143
Reaction score
0
I thought these character entity-references need 4 digits if you want to specify a decimal value (character-code 160 as the &nbsp; in HTML) (and not 3, as in your example).

Try

 

(instead of  )

-- Anton.
 

Collin VanDyck

Usenet Archive
Joined
May 5, 2003
Messages
134
Reaction score
0
Thanks, but same result. I ended up deciding to pass everything through an xml encoder-decoder that would do a regex replaceAll on

&#([0-9+);

to

[unicode]$1[/unicode]

And after the transform was done, reverse it back into the character reference syntax.
 

Maarten Wiltink

Usenet Archive
Joined
Oct 11, 2001
Messages
108
Reaction score
0
I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation.

Probably UTF-8 encoded. Try specifying the encoding to generate in your xsl:eek:utput element. Iso-8859-1 should do what you want.

Groetjes,
Maarten Wiltink
 

Julian Reschke

Usenet Archive
Joined
Apr 5, 2000
Messages
20
Reaction score
0
Collin said:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem..

I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation.

Take this source XML document:

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
some space separated text
</sourcexml>

And this stylesheet:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput method="xml" indent="yes"/> <xsl:template match="@*|*|text|processing-instruction"> <xsl:copy> <xsl:apply-templates select="@*|*|text|processing-instruction"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

I am trying to get it to regurgitate the original document, with the 's intact. Instead I am getting bizarre characters (copied from windows CMD window):

That's how the XPath data model works - the information about whether a character originally was entered as a numerical entitity isn't available
- so there's no chance to preserve that bit of information using XSLT.

(amazing how many wrong suggestions were made :)

Julian
 

yzzzzz

Usenet Archive
Joined
Dec 26, 2002
Messages
51
Reaction score
0
Collin said:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem..

I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation.

Take this source XML document:

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
some space separated text
</sourcexml>

And this stylesheet:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput method="xml" indent="yes"/> <xsl:template match="@*|*|text|processing-instruction"> <xsl:copy> <xsl:apply-templates select="@*|*|text|processing-instruction"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

I am trying to get it to regurgitate the original document, with the 's intact. Instead I am getting bizarre characters (copied from windows CMD window):

<?xml version="1.0" encoding="UTF-8"?>
<sourcexml>
someáspaceáseparatedátext
</sourcexml>

This an understandable behaviour:

* The   is parsed by the XML parser, and is considered being a NO-BREAK SPACE Unicode character. See here:
<http://www.unicode.org/charts/PDF/U0080.pdf>, character 0xA0.

* The XML is parsed with the XSL, and the NO-BREAK SPACE Unicode character is kept unchanged in the result XML document.

* The output document is encoded as ISO-8859-1 (Latin 1) for some reason (this is the default character encoding on many platforms), instead of UTF-8, and in ISO-8859-1, the NO-BREAK SPACE character is encoded as a single 0xA0 byte.

* When the CMD window tries to display the character, it understands the XML document as being encoded in the CP437 character set (the *very* old DOS character set, for compatibility). It gets the 0xA0 byte, and in CP437 the 0xA0 byte represents the LATIN SMALL LETTER A WITH ACUTE Unicode character, which is what you see. See here:
<http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP437.TXT>

Of course in UTF-8 the output would have been the folowing bytes:
some space separated text (Latin 1 bytes)
which in CMD would be displayed as something I can't type here, because it invloves 0xc2 'BOX DRAWINGS LIGHT DOWN AND HORIZONTAL' characters. It would probably have looked like this:
some|áspace|áseparated|átext
 

Maarten Wiltink

Usenet Archive
Joined
Oct 11, 2001
Messages
108
Reaction score
0
Maarten Wiltink said:
Probably UTF-8 encoded. Try specifying the encoding to generate in your xsl:eek:utput element. Iso-8859-1 should do what you want.

On second thoughts, it shouldn't. Since U+00a0 is a valid character in iso-8859-1, it will be output verbatim. Asking for output in (7-bits) us-ascii should cause the processor to produce a character entity.

Groetjes,
Maarten Wiltink
 

Richard Tobin

Usenet Archive
Joined
Jan 6, 2000
Messages
4,251
Reaction score
0
Collin VanDyck said:
I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation.

This is normal. *Why* do you want to have it output as  ? It shouldn't make any difference to the programs that use the output, if they read it as XML.

If you want it for readability, you could specify that the output encoding should be ascii.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top