Groups | Blog | Home
all groups > dotnet xml > february 2007 >

dotnet xml : fragment vs. document?



Jan Eliasen
2/14/2007 2:51:28 PM
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

Martin Honnen
2/14/2007 3:15:01 PM
[quoted text, click to view]

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
Jan Eliasen
2/14/2007 4:14:11 PM
On Wed, 14 Feb 2007 15:15:01 +0100, Martin Honnen <mahotrash@yahoo.de>
[quoted text, click to view]
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

Martin Honnen
2/14/2007 4:27:30 PM
[quoted text, click to view]

Can you show us the stylesheet?

--

Martin Honnen --- MVP XML
Jan Eliasen
2/14/2007 10:16:17 PM
On Wed, 14 Feb 2007 16:27:30 +0100, Martin Honnen <mahotrash@yahoo.de>
[quoted text, click to view]
Sure

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

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

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

Jan Eliasen
2/15/2007 12:00:00 AM
On Wed, 14 Feb 2007 22:16:17 +0100, Jan Eliasen
[quoted text, click to view]
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

Martin Honnen
2/15/2007 4:25:40 PM
[quoted text, click to view]

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
Jan Eliasen
2/15/2007 8:21:49 PM
On Thu, 15 Feb 2007 16:25:40 +0100, Martin Honnen <mahotrash@yahoo.de>
[quoted text, click to view]
Thanks!

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

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

AddThis Social Bookmark Button