473,416 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to make XslCompiledTransform transform &amps; as XslTransform does

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;
}

Nov 6 '06 #1
12 10044
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.)

Nov 6 '06 #2
InvalidLastName wrote:
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.

// 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
Nov 6 '06 #3
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]" <so**@body.comwrote in message news:%2****************@TK2MSFTNGP04.phx.gbl...
InvalidLastName wrote:
>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.

>// 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
Nov 6 '06 #4
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-****@online.microsoft.comwrote in message
news:WS**************@TK2MSFTNGXA01.phx.gbl...
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.)

Nov 6 '06 #5
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]" <so**@body.comwrote in message news:%2****************@TK2MSFTNGP04.phx.gbl...
InvalidLastName wrote:
>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.

>// 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
Nov 6 '06 #6
InvalidLastName wrote:
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/
Nov 6 '06 #7
InvalidLastName wrote:
<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/
Nov 6 '06 #8
InvalidLastName wrote:
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
Nov 6 '06 #9
InvalidLastName wrote:
<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
Nov 6 '06 #10
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" <ma*******@yahoo.dewrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
InvalidLastName wrote:
>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/
Nov 6 '06 #11
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]" <so**@body.comwrote in message news:45**************@body.com...
InvalidLastName wrote:
><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
Nov 6 '06 #12
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.)

Nov 7 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim Douglas | last post by:
Do they play well together? Anyone using the Sybase .NET provider. Any problems or should I stick with the Sybase OLEDB? Or worse check with the DataDirect folks for their .NET provider for...
1
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
2
Fary4u
by: Fary4u | last post by:
Hi i'm trying to make 2 different Connection & Recordset in 1 single file But accessing main values from the Query String, this part is working fine but only problem to access the 2nd part of...
8
by: emailmygroup | last post by:
I am trying to decode character ‘#; but it is not working. Anybody knows how to decode this character?
5
by: =?Utf-8?B?Um9iZXJ0?= | last post by:
Hello all, I have an XmlException being thrown trying to use XslCompiledTransform while using the google api. My suspicion is there is some html decoding happening that I want to prevent... ...
2
by: steve.nickels | last post by:
Hello. I'm in the midst of moving a web application from ASP.NET 1.1 to ASP.NET 2.0 (framework 3.0), and as part of this move, I am told by Visual Studio that the XslTransform object is now...
0
by: Mark Gold | last post by:
Hi! We have a VB application using Crystal Reports 6 that has worked successfully on hundreds of systems for over 10 years. Now, on one network, the application and access database does not close....
0
by: anasanjaria | last post by:
here is my xml code: filename: cdcollection.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- Edited by XMLSpy® --> <catalog> <cd> <title>Empire Burlesque</title>
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.