473,507 Members | 12,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XsltCompiledTransform question

Here are three things that I thought would be equivalent but are not. Just
wondering why:

XmlElement foo = doc.CreateElement("foo");
// load it up with a body
xslt.Transform (foo, null, new XmlTextWriter(new StringWriter()));
xslt.Transform (foo.CreateNavigator(), null, new XmlTextWriter(new
StringWriter()));
xslt.Transform(new XmlNodeReader(foo), new XmlTextWriter(new StringWriter()));

Only the third one applies the transform and produces the expected output.
The first two produce nothing.

The docs say that passing in an XmlNode or navigator "usually an XmlDocument
or an XPathDocument"; it doesn't say the transform won't be applied *unless*
it's an XmlDocument.

I understand the XmlNodeReader is hiding the fact that it's starting at
something other than a document root.

I know this is an peculiar use case but was curious why the first 2 produced
nothing?

Thanks
Mark

Jun 27 '08 #1
8 2040
Mark wrote:
Here are three things that I thought would be equivalent but are not. Just
wondering why:

XmlElement foo = doc.CreateElement("foo");
// load it up with a body
xslt.Transform (foo, null, new XmlTextWriter(new StringWriter()));
xslt.Transform (foo.CreateNavigator(), null, new XmlTextWriter(new
StringWriter()));
xslt.Transform(new XmlNodeReader(foo), new XmlTextWriter(new StringWriter()));

Only the third one applies the transform and produces the expected output.
The first two produce nothing.

The docs say that passing in an XmlNode or navigator "usually an XmlDocument
or an XPathDocument"; it doesn't say the transform won't be applied *unless*
it's an XmlDocument.

I understand the XmlNodeReader is hiding the fact that it's starting at
something other than a document root.

I know this is an peculiar use case but was curious why the first 2 produced
nothing?
I am sure you can pass in an XmlElement node or an XPathNavigator
created from an XmlElement.
For instance when XSLTFile1.xslt is the identity transformation

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

then the following code

XslCompiledTransform xsltProc = new XslCompiledTransform();
xsltProc.Load(@"..\..\XSLTFile1.xslt");

XmlDocument doc = new XmlDocument();
XmlElement foo = doc.CreateElement("foo");

xsltProc.Transform(foo, null, Console.Out);
Console.WriteLine();

xsltProc.Transform(foo.CreateNavigator(), null, Console.Out);
Console.WriteLine();

outputs

<?xml version="1.0" encoding="ibm850"?><foo />
<?xml version="1.0" encoding="ibm850"?><foo />

It is not clear to me how your sample code checks any output if you
simply pass in new XmlTextWriter(new StringWriter()) as the third
argument to the Transform method. Where would you see the transformation
result in that case?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Hi Martin,

I was summarizing before; I really had
StringWriter sw = new StringWriter();
StringBuilder sb = sw.GetStringBuilder();
so I could check the output.

The issue is probably my stylesheet.
<xsl:template match="/foo">

When I pass in a dangling node, it's not matching the /. When I use an
XmlNodeReader, it can't tell the difference...

Thanks
Mark

"Martin Honnen" wrote:
Mark wrote:
Here are three things that I thought would be equivalent but are not. Just
wondering why:

XmlElement foo = doc.CreateElement("foo");
// load it up with a body
xslt.Transform (foo, null, new XmlTextWriter(new StringWriter()));
xslt.Transform (foo.CreateNavigator(), null, new XmlTextWriter(new
StringWriter()));
xslt.Transform(new XmlNodeReader(foo), new XmlTextWriter(new StringWriter()));

Only the third one applies the transform and produces the expected output.
The first two produce nothing.

The docs say that passing in an XmlNode or navigator "usually an XmlDocument
or an XPathDocument"; it doesn't say the transform won't be applied *unless*
it's an XmlDocument.

I understand the XmlNodeReader is hiding the fact that it's starting at
something other than a document root.

I know this is an peculiar use case but was curious why the first 2 produced
nothing?

I am sure you can pass in an XmlElement node or an XPathNavigator
created from an XmlElement.
For instance when XSLTFile1.xslt is the identity transformation

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

then the following code

XslCompiledTransform xsltProc = new XslCompiledTransform();
xsltProc.Load(@"..\..\XSLTFile1.xslt");

XmlDocument doc = new XmlDocument();
XmlElement foo = doc.CreateElement("foo");

xsltProc.Transform(foo, null, Console.Out);
Console.WriteLine();

xsltProc.Transform(foo.CreateNavigator(), null, Console.Out);
Console.WriteLine();

outputs

<?xml version="1.0" encoding="ibm850"?><foo />
<?xml version="1.0" encoding="ibm850"?><foo />

It is not clear to me how your sample code checks any output if you
simply pass in new XmlTextWriter(new StringWriter()) as the third
argument to the Transform method. Where would you see the transformation
result in that case?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #3
Mark wrote:
I was summarizing before; I really had
StringWriter sw = new StringWriter();
StringBuilder sb = sw.GetStringBuilder();
so I could check the output.

The issue is probably my stylesheet.
<xsl:template match="/foo">

When I pass in a dangling node, it's not matching the /. When I use an
XmlNodeReader, it can't tell the difference...
The XmlNodeReader reparses the DOM so it makes some sense that it
presents the foo element as a child of the root node /.
For the other two cases (passing in the sole XmlElement or the
XPathNavigator created on the sole XmlElement) the foo element has no
root so your match="/foo" does not apply.
Obviously it is an edge case but if you can change your stylesheet to
make sure you use match="foo" it should work as needed.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #4
Hi Mark,

I think your analysis is reasonable. The document root based path (
"/....") make a single node not be able to be resolved. While using
XmlNodeReader, it internally generate a document for the given XmlNode
which makes the difference.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: =?Utf-8?B?TWFyaw==?= <mm******@nospam.nospam>
References: <9A**********************************@microsoft.co m>
<el**************@TK2MSFTNGP04.phx.gbl>
>Subject: Re: XsltCompiledTransform question
Date: Fri, 13 Jun 2008 10:02:00 -0700
>
Hi Martin,

I was summarizing before; I really had
StringWriter sw = new StringWriter();
StringBuilder sb = sw.GetStringBuilder();
so I could check the output.

The issue is probably my stylesheet.
<xsl:template match="/foo">

When I pass in a dangling node, it's not matching the /. When I use an
XmlNodeReader, it can't tell the difference...

Thanks
Mark

"M
Jun 27 '08 #5
Hi Martin...

I know this is wandering a bit afield, but after this discussion I got
curious how the xpath operations on XmlNodes would respond, so I made a
little test:

XmlDocument selTest = new XmlDocument();
XmlElement selElm;
selTest.LoadXml("<foo><a><b><c><b>hi</b></c></b></a></foo>");
XmlNodeList selList = selTest.SelectNodes("//b");
selElm = selTest.DocumentElement.FirstChild as XmlElement;
selList = selElm.SelectNodes("//b");
selList = selList[0].SelectNodes("//b");
selElm = selTest.CreateElement("d");
selElm.InnerXml = "<b>in d</b>";
selList = selElm.SelectNodes("//b");

In this case, I was trying the "anywhere in tree" selection. I tried it on
the document root, a couple of elements down, and then in a dangling element.

Within the dom tree, I get the same 2 nodes no matter what node I make the
selection from.

From our prior discussion, I didn't know what to expect from the dangling
node. In the case of the dangling (unrooted) element, selecting "//b"
essentially treated the dangling node as the root and found the child.

Is this a fundamentally different case that the one we talked about before?

Thanks
Mark

Jun 27 '08 #6
Mark wrote:
From our prior discussion, I didn't know what to expect from the dangling
node. In the case of the dangling (unrooted) element, selecting "//b"
essentially treated the dangling node as the root and found the child.

Is this a fundamentally different case that the one we talked about before?
The results astonish me.

Here is a test:

XmlDocument doc = new XmlDocument();

XmlElement foo = doc.CreateElement("foo");
foo.InnerXml = "<bar>baz</bar>";

XmlNode rootNode = foo.SelectSingleNode("/");
Console.WriteLine("rootNode.NodeType: {0}; LocalName: {1}",
rootNode.NodeType, rootNode.LocalName);

XPathNavigator fooNav = foo.CreateNavigator();
XPathNavigator rootNav = fooNav.SelectSingleNode("/");
Console.WriteLine("rootNav.NodeType: {0}; LocalName: {1}",
rootNav.NodeType, rootNav.LocalName);

Output for me with Visual Studio 2005:

rootNode.NodeType: Element; LocalName: foo
rootNav.NodeType: Element; LocalName: foo
So for the XPath API the dangling element node has itself as its root
node selected by the XPath "/". If you select "/*" then you get the
'bar' element as

XmlDocument doc = new XmlDocument();

XmlElement foo = doc.CreateElement("foo");
foo.InnerXml = "<bar>baz</bar>";

XmlNode rootElement = foo.SelectSingleNode("/*");
Console.WriteLine("rootElement.NodeType: {0}; LocalName:
{1}", rootElement.NodeType, rootElement.LocalName);

XPathNavigator fooNav = foo.CreateNavigator();
XPathNavigator rootElNav = fooNav.SelectSingleNode("/*");
Console.WriteLine("rootElNav.NodeType: {0}; LocalName:
{1}", rootElNav.NodeType, rootElNav.LocalName);

outputs

rootElement.NodeType: Element; LocalName: bar
rootElNav.NodeType: Element; LocalName: bar
That "/" selects an element node is rather odd in my view.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #7
I was scratching my head as well. Our resident xpath/xslt guru here saw
"//b" as "descendent or self" which the XmlNode.SelectNodes() was decidedly
not doing as well. With my first doc, I got the same 2 results no matter
which node I started with, so it was running back up to the root and then
traversing down.

Given the behavior we saw in xslt with dangling nodes, I was surprised to
find the XmlNode.SelectNodes() behaving it ways that seemed quite
inconsistent with that...

Thanks
Mark
"Martin Honnen" wrote:
Mark wrote:
From our prior discussion, I didn't know what to expect from the dangling
node. In the case of the dangling (unrooted) element, selecting "//b"
essentially treated the dangling node as the root and found the child.

Is this a fundamentally different case that the one we talked about before?

The results astonish me.

Here is a test:

XmlDocument doc = new XmlDocument();

XmlElement foo = doc.CreateElement("foo");
foo.InnerXml = "<bar>baz</bar>";

XmlNode rootNode = foo.SelectSingleNode("/");
Console.WriteLine("rootNode.NodeType: {0}; LocalName: {1}",
rootNode.NodeType, rootNode.LocalName);

XPathNavigator fooNav = foo.CreateNavigator();
XPathNavigator rootNav = fooNav.SelectSingleNode("/");
Console.WriteLine("rootNav.NodeType: {0}; LocalName: {1}",
rootNav.NodeType, rootNav.LocalName);

Output for me with Visual Studio 2005:

rootNode.NodeType: Element; LocalName: foo
rootNav.NodeType: Element; LocalName: foo
So for the XPath API the dangling element node has itself as its root
node selected by the XPath "/". If you select "/*" then you get the
'bar' element as

XmlDocument doc = new XmlDocument();

XmlElement foo = doc.CreateElement("foo");
foo.InnerXml = "<bar>baz</bar>";

XmlNode rootElement = foo.SelectSingleNode("/*");
Console.WriteLine("rootElement.NodeType: {0}; LocalName:
{1}", rootElement.NodeType, rootElement.LocalName);

XPathNavigator fooNav = foo.CreateNavigator();
XPathNavigator rootElNav = fooNav.SelectSingleNode("/*");
Console.WriteLine("rootElNav.NodeType: {0}; LocalName:
{1}", rootElNav.NodeType, rootElNav.LocalName);

outputs

rootElement.NodeType: Element; LocalName: bar
rootElNav.NodeType: Element; LocalName: bar
That "/" selects an element node is rather odd in my view.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #8
Hi Mark,

I think the different behavior in XmlNode.Select with "//b" like path might
be caused by the difference context between XmlNode and XmlDocument. Since
XmlDocument has an entireo DOM context(start from root). And the "//b" like
path may also require a search from root to all descendent nodes.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Thread-Topic: XsltCompiledTransform question
thread-index: AcjQnqeS97g6+D1NSG+IYJh/7hksDQ==
X-WBNR-Posting-Host: 65.55.21.8
From: =?Utf-8?B?TWFyaw==?= <mm******@nospam.nospam>
References: <9A**********************************@microsoft.co m>
<el**************@TK2MSFTNGP04.phx.gbl<85E372C4-C273-4C72-
>
I was scratching my head as well. Our resident xpath/xslt guru here saw
"//b" as "descendent or self" which the XmlNode.SelectNodes() was
decidedly
>not doing as well. With my first doc, I got the same 2 results no matter
which node I started with, so it was running back up to the root and then
traversing down.

Given the behavior we saw in xslt with dangling nodes, I was surprised to
find the XmlNode.SelectNodes() behaving it ways that seemed quite
inconsistent with that...

Thanks
Mark
"Martin Honnen" wrote:
>Mark wrote:
From our prior discussion, I didn't know what to expect from the
dangling
node. In the case of the dangling (unrooted) element, selecting "//b"
essentially treated the dangling node as the root and found the child.

Is this a fundamentally different case that the one we talked about
before?
>>
The results astonish me.

Here is a test:

XmlDocument doc = new XmlDocument();

XmlElement foo = doc.CreateElement("foo");
foo.InnerXml = "<bar>baz</bar>";

XmlNode rootNode = foo.SelectSingleNode("/");
Console.WriteLine("rootNode.NodeType: {0}; LocalName: {1}",
rootNode.NodeType, rootNode.LocalName);

XPathNavigator fooNav = foo.CreateNavigator();
XPathNavigator rootNav = fooNav.SelectSingleNode("/");
Console.WriteLine("rootNav.NodeType: {0}; LocalName: {1}",
rootNav.NodeType, rootNav.LocalName);

Output for me with Visual Studio 2005:

rootNode.NodeType: Element; LocalName: foo
rootNav.NodeType: Element; LocalName: foo
So for the XPath API the dangling element node has itself as its root
node selected by the XPath "/". If you select "/*" then you get the
'bar' element as

XmlDocument doc = new XmlDocument();

XmlElement foo = doc.CreateElement("foo");
foo.InnerXml = "<bar>baz</bar>";

XmlNode rootElement = foo.SelectSingleNode("/*");
Console.WriteLine("rootElement.NodeType: {0}; LocalName:
{1}", rootElement.NodeType, rootElement.LocalName);

XPathNavigator fooNav = foo.CreateNavigator();
XPathNavigator rootElNav = fooNav.SelectSingleNode("/*");
Console.WriteLine("rootElNav.NodeType: {0}; LocalName:
{1}", rootElNav.NodeType, rootElNav.LocalName);

outputs

rootElement.NodeType: Element; LocalName: bar
rootElNav.NodeType: Element; LocalName: bar
That "/" selects an element node is rather odd in my view.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #9

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

Similar topics

3
4996
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2628
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
3058
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
3389
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
3682
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
4020
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
0
930
by: bennett | last post by:
Hi All I have just been reading up on improving the speed of transforming XML using XsltCompiledTransform in .NET 2.0, and I have read if you set the XsltCompiledTransform processor as a static or...
0
800
by: bennett | last post by:
Hi All Does anyone know of any 3rd party dll's that I can use to transform large XML (typically 3 - 5 MB in size with a complex structure) documents more efficiently than XsltCompiledTransform? ...
0
810
by: amollokhande1 | last post by:
Hi All, Following is a piece of code that loads the XSLT xslt = New XslCompiledTransform(False) xslt.Load(objPatternTemplate, objXSLTSettings, New XmlUrlResolver) Now I want to modify...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7314
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,...
1
7030
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
7482
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
5623
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,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.