Connecting Tech Pros Worldwide Forums | Help | Site Map

How to make XslCompiledTransform transform &amps; as XslTransform does

InvalidLastName
Guest
 
Posts: n/a
#1: Nov 6 '06
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example:

// javascript
if (a > b)
.....

// xsl contents
abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />



However, since we switched to replace XslTransform with XslCompiledTransform, all these entity classes need to be wrapped within xsl:text with DOE
<xsl:text disable-output-escaping="yes">

Results from XslTransform
if (a b)
....
abc.aspx?p1=v1&p2=...

Results from XslCompiledTransform without be wrapped within xsl:text without doe
if (a &gt; b)
....
abc.aspx?p1=v1&amp;p2=...

In our implementations, all the XML contents were "converted into XpathDocument, and the output method of xsl file is set to "html" and I have tried to use 1- StringWriter, 2- XmlWritter, 3- MemoryStream (see below), and we got the same results from all three approaches

Is there a way that we can make the XslCompiledTransform handle those entity classes, e.g. &amp;, &gt;, &lt; , as XslTransform


// using StringWriter version
public static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver )
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);
xt.Transform(nav, PrepareXsltArgumentList(htArguments), sw);
sw.Close();
xt = null;
return sb.ToString();
}


// using xml writer
public static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver )
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);
XmlWriterSettings settings = xt.OutputSettings.Clone();
settings.CheckCharacters = false;
settings.CloseOutput = true;
XmlWriter xw = XmlWriter.Create(sb, settings);
xt.Transform(nav, PrepareXsltArgumentList(htArguments), xw);
xw.Close();
sw.Close();
return sb.ToString();
}


// using memory stream
public static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver )
{
// use memroystream
MemoryStream mry = new MemoryStream();
StreamReader sr = new StreamReader(mry, System.Text.UTF8Encoding.UTF8);

XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);
xt.Transform(nav, PrepareXsltArgumentList(htArguments), mry);

mry.Position = 0;
string s = sr.ReadToEnd();
sr.Close();
return s;

}


private static XslCompiledTransform PrepareCompiledTransform(string xslPath, XmlResolver stylesheetResolver )
{
bool bEnableDebug = false;
XslCompiledTransform xt = new XslCompiledTransform(bEnableDebug);

XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
XmlResolver theResolver = (stylesheetResolver == null ? new XmlUrlResolver() : stylesheetResolver);
xt.Load(GetXmlFilePath(xslPath), settings, theResolver);
return xt;
}


Kevin Yu [MSFT]
Guest
 
Posts: n/a
#2: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Hi,

The disable-output-escaping is in a gray area

From the XSLT 1.0 Spec http://www.w3.org/TR/xslt#disable-output-escaping:

"Since disabling output escaping may not work with all XSLT processors and
can result in XML that is not well-formed, it should be used only when there
is no alternative."

So an XSLT processor is not required to support d-o-e.

In this case, you can try to use a StreamWriter to write out the results.

Please check the following links for more information about this issue.

http://msdn2.microsoft.com/en-us/lib...k9(VS.80).aspx
http://www.thescripts.com/forum/thread534528.html
http://forums.microsoft.com/MSDN/Sho...72483&SiteID=1

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Oleg Tkachenko [MVP]
Guest
 
Posts: n/a
#3: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


InvalidLastName wrote:
Quote:
We have been used XslTransform. .NET 1.1, for transform XML document,
Dataset with xsl to HTML. Some of these html contents contain javascript
and links. For example:
>
// javascript
if (a &gt; b)
Script should go out with no escaping. Just make sure you output HTML -
set <xsl:output method="html"and output into Stream or TextWriter.

Quote:
// xsl contents
abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
In HTML & in links must be escaped as &amp; Don't put efforts in
producing malformed HTML.


--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
InvalidLastName
Guest
 
Posts: n/a
#4: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Thank for the reply.

However, Firefox has trouble to recognize &amp; in the link as &. All our parameters in the QueryString are returned as amp;p1 instead of p1



"Oleg Tkachenko [MVP]" <some@body.comwrote in message news:%23Q87DtYAHHA.4568@TK2MSFTNGP04.phx.gbl...
Quote:
InvalidLastName wrote:
Quote:
>We have been used XslTransform. .NET 1.1, for transform XML document,
>Dataset with xsl to HTML. Some of these html contents contain javascript
>and links. For example:
>
>// javascript
>if (a &gt; b)
Script should go out with no escaping. Just make sure you output HTML -
set <xsl:output method="html"and output into Stream or TextWriter.

Quote:
>// xsl contents
>abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
In HTML & in links must be escaped as &amp; Don't put efforts in
producing malformed HTML.


--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
InvalidLastName
Guest
 
Posts: n/a
#5: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Hi Kevin,
I am not try to use d-o-e with XslCompiledTransform. However, since the
behavior changed, I have to use d-o-e to make sure XslCompiledTransform
generate HTML all the browsers can process. IE has no problem to read some
thing like

a.aspx?p1=v1&amp;p2=v1&amp;p3=v3

IE return
p1
p2
p3
in QueryString collection.

But Firefox will return
amp;p1
amp;p2
amp;p3


And neither Browsers understand what is

i f (a &gt; b)

I have tried the memoryStream with output method= html and still got the
same results

Please let me know what to generate HTML contents contain
if (a b)

a.aspx?p1=v1&p2=v2
as well-formed XML


Thanks

Michael

"Kevin Yu [MSFT]" <v-kevy@online.microsoft.comwrote in message
news:WSJTKYYAHHA.5200@TK2MSFTNGXA01.phx.gbl...
Quote:
Hi,
>
The disable-output-escaping is in a gray area
>
From the XSLT 1.0 Spec http://www.w3.org/TR/xslt#disable-output-escaping:
>
"Since disabling output escaping may not work with all XSLT processors and
can result in XML that is not well-formed, it should be used only when
there
is no alternative."
>
So an XSLT processor is not required to support d-o-e.
>
In this case, you can try to use a StreamWriter to write out the results.
>
Please check the following links for more information about this issue.
>
http://msdn2.microsoft.com/en-us/lib...k9(VS.80).aspx
http://www.thescripts.com/forum/thread534528.html
http://forums.microsoft.com/MSDN/Sho...72483&SiteID=1
>
Kevin Yu
Microsoft Online Community Support
>
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
>
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
>

InvalidLastName
Guest
 
Posts: n/a
#6: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Oleg,
I verified my code again. I do have out method set to html and I am using memory Stream. The generated contents for the script block still showed a &gt; b instead of a b

I am not quite sure what I did wrong. Here are code fragments:

## from xsd
<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.microsoft.com/intellisense/ie5"

xmlns:msxsl="urn:schemas-microsoft-com:xslt" >

<xsl:output method="html" encoding="utf-8" />





## from transform method

public static string Transform(System.Xml.XPath.XPathNavigator nav, string xslPath, Hashtable htArguments, XmlResolver resolver )

{

MemoryStream mry = new MemoryStream();

StreamReader sr = new StreamReader(mry, System.Text.UTF8Encoding.UTF8);

XslCompiledTransform xt = PrepareCompiledTransform(xslPath, resolver);

xt.Transform(nav, PrepareXsltArgumentList(htArguments), mry);

mry.Position = 0;

string s = sr.ReadToEnd();

sr.Close();

return s;


}



Thanks



"Oleg Tkachenko [MVP]" <some@body.comwrote in message news:%23Q87DtYAHHA.4568@TK2MSFTNGP04.phx.gbl...
Quote:
InvalidLastName wrote:
Quote:
>We have been used XslTransform. .NET 1.1, for transform XML document,
>Dataset with xsl to HTML. Some of these html contents contain javascript
>and links. For example:
>
>// javascript
>if (a &gt; b)
Script should go out with no escaping. Just make sure you output HTML -
set <xsl:output method="html"and output into Stream or TextWriter.

Quote:
>// xsl contents
>abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
In HTML & in links must be escaped as &amp; Don't put efforts in
producing malformed HTML.


--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Martin Honnen
Guest
 
Posts: n/a
#7: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


InvalidLastName wrote:
Quote:
I am not try to use d-o-e with XslCompiledTransform. However, since the
behavior changed, I have to use d-o-e to make sure XslCompiledTransform
generate HTML all the browsers can process. IE has no problem to read some
thing like
>
a.aspx?p1=v1&amp;p2=v1&amp;p3=v3
>
IE return
p1
p2
p3
in QueryString collection.
>
But Firefox will return
amp;p1
amp;p2
amp;p3
That is not true, Firefox will certainly handle a properly escaped
ampersand in a HTML document, see the link in
<http://www34.brinkster.com/libertydevelop/asp/test2006110601.html>
for an example.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin Honnen
Guest
 
Posts: n/a
#8: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


InvalidLastName wrote:
Quote:
<xsl:stylesheet version="1.0"
>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/intellisense/ie5"
Take that xmlns declaration out, html output method applies to elements
in no namespace while your default namespace declaration xmlns="..."
puts the element in a namespace and that way the html output method does
not apply to the elements your stylesheet generates.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oleg Tkachenko [MVP]
Guest
 
Posts: n/a
#9: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


InvalidLastName wrote:
Quote:
However, Firefox has trouble to recognize &amp; in the link as &. All
our parameters in the QueryString are returned as *amp;p1* instead of *p1*
Sorry, but I hardly believe Firefox doesn't support such basic HTML
specification requirement. I'm sure it does. You must be having double
escaping problem - chances are your output is &amp;amp;p1.

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Oleg Tkachenko [MVP]
Guest
 
Posts: n/a
#10: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


InvalidLastName wrote:
Quote:
<xsl:stylesheet version="1.0"
>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/intellisense/ie5"
Yeah, Martin is right. This declaration drives XslCompiledTransform
crazy. It actually makes your output elements to be not HTML, but custom
XML and so XslCompiledTransform doesn't apply HTML output rules.

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
InvalidLastName
Guest
 
Posts: n/a
#11: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Hi Martin,
Thanks for pointing this out. Yes, you are correct. The Firefox works when the &amp; embedded the <A href>

I did more researches on why I got the Firefox issue at first place, and it turned that our page was opened by Javascript as
var s = "test.aspx?p1=v1&amp;p2=v2&amp;p3=v3";
document.location = s; //

Please see following full example:

<html>
<head>
</head>
<body>
<a href="test.aspx?p1=v1&amp;p2=v2&amp;p3=v3">this will work in IE and Firefox<a>


<script>
function openWin()
{
var s = "test.aspx?p1=v1&amp;p2=v2&amp;p3=v3";
document.location = s;
//window.open(s, "popup", "height=500, width=700, menubar, scrollbars, resizable, status");
}
</script>
<br/>
<a href="javascript:openWin()">this will not work in Firefox</a>
</body>
</html>


In Firefox:
result from first link: http://localhost/test.aspx?p1=v1&p2=v2&p3=v3

result from second link: http://localhost/test.aspx?p1=v1&amp;p2=v2&amp;p3=v3


Any suggestion is really appreciated. Thanks

- ILN


"Martin Honnen" <mahotrash@yahoo.dewrote in message news:%23o3C7kaAHHA.3536@TK2MSFTNGP03.phx.gbl...
Quote:
InvalidLastName wrote:
Quote:
>I am not try to use d-o-e with XslCompiledTransform. However, since the
>behavior changed, I have to use d-o-e to make sure XslCompiledTransform
>generate HTML all the browsers can process. IE has no problem to read some
>thing like
>
>a.aspx?p1=v1&amp;p2=v1&amp;p3=v3
>
>IE return
>p1
>p2
>p3
>in QueryString collection.
>
>But Firefox will return
>amp;p1
>amp;p2
>amp;p3
That is not true, Firefox will certainly handle a properly escaped
ampersand in a HTML document, see the link in
<http://www34.brinkster.com/libertydevelop/asp/test2006110601.html>
for an example.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
InvalidLastName
Guest
 
Posts: n/a
#12: Nov 6 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Oleg and Martin,
Thank both of you. This is not the first time I got helps from you two =).

After I taking out the xml declaration xmlns="http://schemas.microsoft.com/intellisense/ie5", everything start working just like old XslTransform

The reason we had that declaration is for intellisense on HTML elements in .NET 1.1. That does mess up the element namespace =(

Really appreciate it


ILN


"Oleg Tkachenko [MVP]" <some@body.comwrote in message news:454F4DA2.5010809@body.com...
Quote:
InvalidLastName wrote:
Quote:
><xsl:stylesheet version="1.0"
>
>xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>xmlns="http://schemas.microsoft.com/intellisense/ie5"
Yeah, Martin is right. This declaration drives XslCompiledTransform
crazy. It actually makes your output elements to be not HTML, but custom
XML and so XslCompiledTransform doesn't apply HTML output rules.

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Kevin Yu [MSFT]
Guest
 
Posts: n/a
#13: Nov 7 '06

re: How to make XslCompiledTransform transform &amps; as XslTransform does


Hi ILN,

I'm sorry, but I don't have FireFox for testing.

It seems that you have resolved the problem by removing
xmlns="http://schemas.microsoft.com/intellisense/ie5". If you have any
other question, please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Closed Thread