Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stylesheet just won't work with .NET! - warning - causing grey hairs

0 views
Skip to first unread message

IMarshal

unread,
Apr 4, 2002, 5:35:57 PM4/4/02
to
I have a stylesheet (given below) that's sole purpose is
to transform a UTF-8 document to us-ascii using Numeric
Character References (NCRs). This allows our software
that's not Unicode compatible to at least work with it -
to some degree.

However, *ALL I GET* is UTF-8 - it's like .NET
XslTransform is somehow ignoring the new encoding.
I've tried changing the XmlTextWriter encoding from UTF8
to ASCII but neither helps.

Please Help!

Here's my code and stylesheet:

XPathDocument xmlDoc;
XslTransform xslT;
XmlTextWriter myWriter;

myWriter = new XmlTextWriter(
Server.MapPath( "outXML.xml" ),
System.Text.Encoding.UTF8 );

xmlDoc = new XPathDocument(
Server.MapPath( "XMLCompareFiles\\s2v21chp8f11529.xml" ) );

xslT = new XslTransform();

xslT.Load( Server.MapPath
( "Transform2NCR.xslt" ) );
xslT.Transform( xmlDoc, null,
myWriter );

myWriter.Close();


<!----- STYLESHEET ---------->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="us-ascii" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node
()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Joe Feser

unread,
Apr 4, 2002, 5:56:13 PM4/4/02
to
Try changing the output to this

<xsl:output omit-xml-declaration="no" method="xml" media-type="text/xml"
indent="no" encoding="iso8859-1"/>

If that doesn't work, try using a StringWriter and not the xmlTextWritter

If that doesn't work do the Transform that takes in two urls (files) and If
i remember, that does not have the encoding problem.

You can also Interop MSXML4 and get rid of all the problems.

Do you need samples?

Joe


"IMarshal" <imar...@swbell.net> wrote in message
news:b06a01c1dc29$16883680$b1e62ecf@tkmsftngxa04...

IMarshal

unread,
Apr 4, 2002, 6:35:26 PM4/4/02
to
Joe, thanks for the prompt reply - the wife now wonders if
Im losing my sanity. :)

I tried the new xsl:output directive but it didn't work.
I've now written three procedures, each trying to do the
transform in a different way. One uses an XmlTextWriter
for the stream, another uses StringWriter and the newest
uses MSXML4.0.

Even the MSXML4.0 implementation isn't working for me.
Here's my code (Please give samples!!) :)

FreeThreadedDOMDocument40 xmlDoc = new
FreeThreadedDOMDocument40();
FreeThreadedDOMDocument40 xslDoc =
new FreeThreadedDOMDocument40();
XSLTemplate40 xslT = new
XSLTemplate40();

xmlDoc.async = false;
xmlDoc.load( Server.MapPath
( "XMLCompareFiles\\s2v21chp8f11529.xml" ) );

xslDoc.async = false;
xslDoc.load( Server.MapPath
( "Transform2NCR.xslt" ) );

xslT.stylesheet = xslDoc;

IXSLProcessor myProcessor =
xslT.createProcessor();
myProcessor.input = xmlDoc;
myProcessor.transform();

StreamWriter myStream = new
StreamWriter( Server.MapPath( "outXML.xml" ), false,
System.Text.Encoding.UTF8 );

myStream.Write(
myProcessor.output );
myStream.Close();

>.
>

Joe Feser

unread,
Apr 5, 2002, 1:36:01 AM4/5/02
to
Can you email your samples, the entire project.


Joe

"IMarshal" <imar...@swbell.net> wrote in message

news:586c01c1dc31$65ccbfb0$37ef2ecf@TKMSFTNGXA13...

Joe Feser

unread,
Apr 5, 2002, 7:13:24 PM4/5/02
to
I will have the solution posted to http://www.fesersoft.com/newsgroups/ by
the end of the day.

The only solution without writing to the drive was to use MSXML4 and the
ADODB.Stream, setting the charset to "iso-8859-1".

Then returning from the function as a string using the oStream.ReadText()
method.

Joe


"Joe Feser" <nn...@fesersoft.com> wrote in message
news:ue6oHvG3BHA.352@tkmsftngp05...

Joe Feser

unread,
Apr 5, 2002, 7:10:17 PM4/5/02
to
I need a broom to sweep up the lost hair on this one.

I only find out IMarshal is IPersist.??

No clue. Yah trust me I know :).

Joe

"Joe Feser" <nn...@fesersoft.com> wrote in message
news:ue6oHvG3BHA.352@tkmsftngp05...

IMarshal

unread,
Apr 8, 2002, 7:03:46 PM4/8/02
to
Joe, thanks so much for your help - it was invaluable.

The ADODB Stream did the trick. :) I hope you received
my C# translation of the VB.NET code.

>> Í{ wÀ ? ,q¾ Ö= tñø ìC >

Matt Warren [MSFT]

unread,
Apr 9, 2002, 12:48:40 AM4/9/02
to
It looks like you are explicitly telling the writer to use UTF8 encoding.
Try giving it the encoding object you want. You should not need a transform
at all. Either load it into an XmlDocument and save it back out or simply
loop over the reader and write each successive node to the writer using the
WriteNode() method. I'll try this at work tomorrow and post a snippet that
works.

"IMarshal" <imar...@swbell.net> wrote in message
news:b06a01c1dc29$16883680$b1e62ecf@tkmsftngxa04...

IMarshal

unread,
Apr 10, 2002, 2:41:13 AM4/10/02
to
Matt, did you have any luck with it? If you have some
code to post that would be fantastic. Through Joe's help
we were able to get it to work but ONLY with an ADODB
stream.

If I were to reload the xml data back into an XMLDOM
object and then view the data, it transformed the NCRs
back to the character equivilants - even if the encoding
is set to iso-8859-1. ugh

>.
>

Joe Feser

unread,
Apr 10, 2002, 2:49:53 AM4/10/02
to
I tried writing code for what Matt described and it still doesn't work.

Joe must me the man. I wonder how much hair is left?

IPersistStream


"IMarshal" <imar...@swbell.net> wrote in message

news:087601c1e05a$b50d49f0$35ef2ecf@TKMSFTNGXA11...

IMarshal

unread,
Apr 10, 2002, 12:19:15 PM4/10/02
to
hahaha Yeah, I'm to the point I won't have much hair left
at all :( Buy stock in Johnson & Johnson - I think a
few of us will have to be buying Rogaine soon.

Matt, if you get a chance to post the code you're talking
about please do - or let us know the status. My emotional
stability is now brought into question from this whole
affair.

>.
>

Joe Feser

unread,
Apr 10, 2002, 3:31:05 PM4/10/02
to

"IMarshal" <imar...@swbell.net> wrote in message
news:0ec601c1e0ab$75426700$3bef2ecf@TKMSFTNGXA10...

> Matt, if you get a chance to post the code you're talking
> about please do - or let us know the status. My emotional
> stability is now brought into question from this whole
> affair.

Well I'm glad you failed the Postal Worker exam. :)

ISerializable


Steve W. Brewer

unread,
Jul 10, 2002, 5:31:46 AM7/10/02
to
"Joe Feser" <nn...@fesersoft.com> wrote in message
news:OfOw#9P3BHA.2856@tkmsftngp05...

>
> "IMarshal" <imar...@swbell.net> wrote in message
> news:b06a01c1dc29$16883680$b1e62ecf@tkmsftngxa04...
> >
> > I have a stylesheet (given below) that's sole purpose is
> > to transform a UTF-8 document to us-ascii using Numeric
> > Character References (NCRs). This allows our software
> > that's not Unicode compatible to at least work with it -
> > to some degree.
> >
> > However, *ALL I GET* is UTF-8 - it's like .NET
> > XslTransform is somehow ignoring the new encoding.
> > I've tried changing the XmlTextWriter encoding from UTF8
> > to ASCII but neither helps.
> >
> > Please Help!
> >
> > Here's my code and stylesheet:
> >
> > XPathDocument xmlDoc;
> > XslTransform xslT;
> > XmlTextWriter myWriter;
> >
> > myWriter = new XmlTextWriter(
> > Server.MapPath( "outXML.xml" ),
> > System.Text.Encoding.UTF8 );
> >
> > xmlDoc = new XPathDocument(
> > Server.MapPath( "XMLCompareFiles\\s2v21chp8f11529.xml" ) );

> >
> > xslT = new XslTransform();
> >
> > xslT.Load( Server.MapPath
> > ( "Transform2NCR.xslt" ) );
> > xslT.Transform( xmlDoc, null,
> > myWriter );
> >
> > myWriter.Close();
> >
> >
> > <!----- STYLESHEET ---------->
> > <xsl:stylesheet
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > version="1.0">
> > <xsl:output method="xml" encoding="us-ascii" />
> > <xsl:template match="node()|@*">
> > <xsl:copy>
> > <xsl:apply-templates select="node
> > ()" />
> > </xsl:copy>
> > </xsl:template>
> > </xsl:stylesheet>
>
>
> I will have the solution posted to http://www.fesersoft.com/newsgroups/ by
> the end of the day.
>
> The only solution without writing to the drive was to use MSXML4 and the
> ADODB.Stream, setting the charset to "iso-8859-1".
>
> Then returning from the function as a string using the oStream.ReadText()
> method.


I had a similar problem. The solution I came up with was to write a custom
Encoding class, ISO88591WithNCREncoding (yeah, I know it's an awkward name),
to do the job. By passing an instance of this class to any function
accepting a System.Text.Encoding parameter, ISO-8859-1 encoded text with
embedded NCRs can be automatically generated or read.

As an example, here is a snippet of code from the application I was writing
that required this functionality (non-relevant code, such as error checking,
removed for clarity). This snippet transforms an XML file specified by
xmlFilename using an XSLT file specified by xsltFilename and writes the
result to an ISO-8859-1 encoded file with NCRs specified by outputFilename.

XslTransform xslt = new XslTransform();
xslt.Load(xsltFilename);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilename);
StreamWriter f = new StreamWriter(outputFilename, false, new
ISO88591WithNCREncoding());
xslt.Transform(xmlDoc, null, f);

Steve W. Brewer
(Remove the hyphens from my email address to send email.)


// ======================================================================
// ISO88591WithNCREncoding Class verison 1.0 July 10, 2002
// ======================================================================
// Copyright ©2002 Steve W. Brewer (st-...@ka-tech.com)
//
// Note: The email address above has been altered in an effort to prevent
// spam. To send me email, first remove the two hyphens from the
// email address.
//
// This class provides an encoding that represents Unicode characters
// as an ISO-8859-1 octet stream. Characters not defined in ISO-8851-1
// are represented in the octet stream as standard HTML/XML numeric
// character references (NCRs). Only decimal NCRs are supported in this
// version of the class.
//
// ======================================================================

using System;
using System.Text;

public class ISO88591WithNCREncoding : Encoding {
const string iso88591 = "ISO-8859-1";

public ISO88591WithNCREncoding() : base() {
}

#region Properties
public override string BodyName {
get {
return Encoding.GetEncoding(iso88591).BodyName;
}
}

public override int CodePage {
get {
return Encoding.GetEncoding(iso88591).CodePage;
}
}

public override string EncodingName {
get {
return Encoding.GetEncoding(iso88591).EncodingName;
}
}

public override string HeaderName {
get {
return Encoding.GetEncoding(iso88591).HeaderName;
}
}

public override bool IsBrowserDisplay {
get {
return Encoding.GetEncoding(iso88591).IsBrowserDisplay;
}
}

public override bool IsBrowserSave {
get {
return Encoding.GetEncoding(iso88591).IsBrowserSave;
}
}

public override bool IsMailNewsDisplay {
get {
return Encoding.GetEncoding(iso88591).IsMailNewsDisplay;
}
}

public override bool IsMailNewsSave {
get {
return Encoding.GetEncoding(iso88591).IsMailNewsSave;
}
}

public override string WebName {
get {
return Encoding.GetEncoding(iso88591).WebName;
}
}

public override int WindowsCodePage {
get {
return Encoding.GetEncoding(iso88591).WindowsCodePage;
}
}
#endregion

#region Public Methods
public override int GetMaxByteCount(int charCount) {
return charCount * 8;
}


public override int GetByteCount(char[] chars, int index,
int count) {

return GetBytes(chars, index, count, null, 0, false);
}


public override int GetBytes(char[] chars, int charIndex,
int charCount, byte[] bytes, int byteIndex) {

return GetBytes(chars, charIndex, charCount, bytes, byteIndex,
true);
}


public override int GetMaxCharCount(int byteCount) {
return byteCount;
}


public override int GetCharCount(byte[] bytes, int index,
int count) {

return GetChars(bytes, index, count, null, 0, false);
}


public override int GetChars(byte[] bytes, int byteIndex,
int byteCount, char[] chars, int charIndex) {

return GetChars(bytes, byteIndex, byteCount, chars, charIndex,
true);
}
#endregion

int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex, bool fillBytes) {

char c;
string s;
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
int startByteIndex = byteIndex;

for (; charCount > 0; charCount--, charIndex++) {
c = chars[charIndex];

if (c <= '\u007f' || (c >= '\u00a0' && c <= '\u00ff')) {
if (fillBytes)
bytes[byteIndex] = System.Convert.ToByte(c);
byteIndex++;
}
else {
s = String.Format("&#{0};", System.Convert.ToUInt16(c));
if (fillBytes)
asciiEncoding.GetBytes(s, 0, s.Length, bytes,
byteIndex);
byteIndex += s.Length;
}
}

return byteIndex - startByteIndex;
}

int GetChars(byte[] bytes, int byteIndex, int byteCount,
char[] chars, int charIndex, bool fillChars) {

char b;
int startCharIndex = charIndex;
StringBuilder sb;

for (;; byteCount--, byteIndex++) {
if (byteCount > 0) {
b = System.Convert.ToChar(bytes[byteIndex]);

if (b != '&') {
if (fillChars) chars[charIndex] = b;
charIndex++;
continue;
}
}
else {
break;
}

byteCount--; byteIndex++;

if (byteCount > 0) {
b = System.Convert.ToChar(bytes[byteIndex]);

if (b != '#') {
if (fillChars) chars[charIndex] = '&';
charIndex++;
if (fillChars) chars[charIndex] = b;
charIndex++;
continue;
}
}
else {
if (fillChars) chars[charIndex] = '&';
charIndex++;
break;
}

sb = new StringBuilder();

for (;;) {
byteCount--; byteIndex++;

if (byteCount > 0) {
b = System.Convert.ToChar(bytes[byteIndex]);

if (((b < '0' || b > '9') && b != ';') ||
sb.Length > 0 &&
UInt32.Parse(sb.ToString()) > UInt16.MaxValue ) {

if (fillChars) chars[charIndex] = '&';
charIndex++;
if (fillChars) chars[charIndex] = '#';
charIndex++;
if (fillChars)
sb.ToString().CopyTo(0, chars, charIndex,
sb.Length);
charIndex += sb.Length;
if (fillChars) chars[charIndex] = b;
charIndex++;
break;
}
else if (b != ';') {
sb.Append(b);
}
else {
if (fillChars)
chars[charIndex] =
System.Convert.ToChar(UInt16.Parse(sb.ToString()));
charIndex++;
break;
}
}
else {
if (fillChars) chars[charIndex] = '&';
charIndex++;
if (fillChars) chars[charIndex] = '#';
charIndex++;
if (fillChars)
sb.ToString().CopyTo(0, chars, charIndex,
sb.Length);
charIndex += sb.Length;
break;
}
}
}

return charIndex - startCharIndex;
}
}


0 new messages