473,407 Members | 2,306 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,407 software developers and data experts.

fragment vs. document?

Hi

I have some C# code that loops through an XMLDocument and needs to
apply an XSLT stylesheet to each node in the XML document.

My (simplified) code looks like this:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filename);

XmlNodeList nodeliste =
xmldoc.SelectNodes("/*[local-name()='Guideline' and
namespace-uri()='ns']/*[local-name()='ABIE' and
namespace-uri()='ns']");

foreach (XmlNode node in nodeliste)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
settings.Encoding = Encoding.UTF8;
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;

StringBuilder createdContent = new StringBuilder();

XmlWriter writer;
writer = XmlWriter.Create(createdContent, settings);

XmlUrlResolver resolver = new XmlUrlResolver();

XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load(@"XMLtoHTML.xsl", XsltSettings.TrustedXslt, resolver);

XsltArgumentList arguments = new XsltArgumentList();
arguments.AddParam("country", "", country);

transform.Transform(tempFilename, arguments, writer);
}

If I run this code, the output from the transform method is the
innertext of the XmlNode given to the transform method. And NOT the
output I expected :-)

If I, however, create a new XmlDocument, load it with the
node.OuterXml and save this XmlDocument to disc, then the transform
method produces the correct output, given the new filename as input
instead of the XmlNode.

I'd really like not to ahve to write the node to disc before
performing the transformation.

Can anyone help me with that?

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: ja*@eliasen.dk
Feb 14 '07 #1
7 3638
Jan Eliasen wrote:
I have some C# code that loops through an XMLDocument and needs to
apply an XSLT stylesheet to each node in the XML document.

My (simplified) code looks like this:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filename);

XmlNodeList nodeliste =
xmldoc.SelectNodes("/*[local-name()='Guideline' and
namespace-uri()='ns']/*[local-name()='ABIE' and
namespace-uri()='ns']");

foreach (XmlNode node in nodeliste)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
settings.Encoding = Encoding.UTF8;
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;

StringBuilder createdContent = new StringBuilder();

XmlWriter writer;
writer = XmlWriter.Create(createdContent, settings);

XmlUrlResolver resolver = new XmlUrlResolver();

XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load(@"XMLtoHTML.xsl", XsltSettings.TrustedXslt, resolver);

XsltArgumentList arguments = new XsltArgumentList();
arguments.AddParam("country", "", country);

transform.Transform(tempFilename, arguments, writer);
}

If I run this code, the output from the transform method is the
innertext of the XmlNode given to the transform method. And NOT the
output I expected :-)
But your foreach loop does not even use the XmlNode |node| variable at
all so it is not clear what you are trying to achieve in that loop.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 14 '07 #2
On Wed, 14 Feb 2007 15:15:01 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
>Jan Eliasen wrote:
>I have some C# code that loops through an XMLDocument and needs to
apply an XSLT stylesheet to each node in the XML document.

My (simplified) code looks like this:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filename);

XmlNodeList nodeliste =
xmldoc.SelectNodes("/*[local-name()='Guideline' and
namespace-uri()='ns']/*[local-name()='ABIE' and
namespace-uri()='ns']");

foreach (XmlNode node in nodeliste)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
settings.Encoding = Encoding.UTF8;
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;

StringBuilder createdContent = new StringBuilder();

XmlWriter writer;
writer = XmlWriter.Create(createdContent, settings);

XmlUrlResolver resolver = new XmlUrlResolver();

XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load(@"XMLtoHTML.xsl", XsltSettings.TrustedXslt, resolver);

XsltArgumentList arguments = new XsltArgumentList();
arguments.AddParam("country", "", country);

transform.Transform(tempFilename, arguments, writer);
}

If I run this code, the output from the transform method is the
innertext of the XmlNode given to the transform method. And NOT the
output I expected :-)

But your foreach loop does not even use the XmlNode |node| variable at
all so it is not clear what you are trying to achieve in that loop.
Sorry! Replaece the line
transform.Transform(tempFilename, arguments, writer);
with
transform.Transform(node, arguments, writer);

The first one works, if I have saved the XML into a file given by
tempFilename. The second version doesn't work, ie. just passing the
XmlNode as a parameter to the transform method.

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: ja*@eliasen.dk
Feb 14 '07 #3
Jan Eliasen wrote:
>>XmlNodeList nodeliste =
xmldoc.SelectNodes("/*[local-name()='Guideline' and
namespace-uri()='ns']/*[local-name()='ABIE' and
namespace-uri()='ns']");
transform.Transform(node, arguments, writer);

The first one works, if I have saved the XML into a file given by
tempFilename. The second version doesn't work, ie. just passing the
XmlNode as a parameter to the transform method.
Can you show us the stylesheet?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 14 '07 #4
On Wed, 14 Feb 2007 16:27:30 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
>Can you show us the stylesheet?
Sure

http://www.eliasen.dk/xslt/convertXMLtoHTML.xsl

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: ja*@eliasen.dk
Feb 14 '07 #5
On Wed, 14 Feb 2007 22:16:17 +0100, Jan Eliasen
<el*****@nospam.nospamwrote:
>>Can you show us the stylesheet?
Sure

http://www.eliasen.dk/xslt/convertXMLtoHTML.xsl
Turns out, that I don't have to write the XmlNode to disk to make it
work, I can just create a new XmlDocument, and
xmldoc.LoadXml(node.OuterXml) and use the XmlDocument as a parameter
instead of the XmlNode.

But I'd still like to not having to do that and just use the XmlNode I
have...

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: ja*@eliasen.dk
Feb 15 '07 #6
Jan Eliasen wrote:
Turns out, that I don't have to write the XmlNode to disk to make it
work, I can just create a new XmlDocument, and
xmldoc.LoadXml(node.OuterXml) and use the XmlDocument as a parameter
instead of the XmlNode.

But I'd still like to not having to do that and just use the XmlNode I
have...
The difference is that creating a new document with just that element
makes this element the root node so XPath expression or match patterns like
/*
select/match that element.
On the other hand when you directly pass that element node to the
Transform method then transformation starts with that node and looks for
a matching template but the node is not the root element, it still has
all its ancestors. Thus if your stylesheet tries to process stuff
<xsl:template match="/">
or
<xsl:template match="/*">
those templates are not used, rather any template matching the element
node will be used. So when you pass those nodes to the transform method
you should make sure you have a template matching those nodes.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 15 '07 #7
On Thu, 15 Feb 2007 16:25:40 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
>those templates are not used, rather any template matching the element
node will be used. So when you pass those nodes to the transform method
you should make sure you have a template matching those nodes.
Thanks!

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: ja*@eliasen.dk
Feb 15 '07 #8

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

Similar topics

3
by: Alexander Mikhailian | last post by:
I have an http = new XMLHttpRequest(); that provides me with an http.responseXML. Somewhere deep in the http.responseXML there is a fragment called e.g. mydom that I want to copy with all its...
2
by: Rick Strahl [MVP] | last post by:
Hi all, I need a reality check <g>... I have the following XML fragment: <event sitename="West Wind Demo Link"> <url>http://rasnotebook/wconnect/testpage.wwd?Test2</url> <time>11/17/2003...
2
by: Yuriy | last post by:
Hi, any ideas how to read XML fragment from TextReader? XmlTextReader constructor accepts only Stream or string as source Do I miss something? thanks Yuriy
3
by: Gustaf | last post by:
I'm trying to grasp this little passage from the XBRL spec: "The xlink:href attribute MUST be a URI. The URI MUST point to an XML document or to one or more XML fragments within an XML document....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.