2

Here is what I'm trying to do: I've got an XmlDocument, which is already loaded into memory. I want to apply an xsl transformation to a single node of that document.

here's the code:

var xDoc=GetXmlDocument();
var myNode=xDoc.SelectSingleNode("//node");
var xslTransformer=new XslCompiledTransform(); 
xslTransformer.Load(new XmlTextReader(new StringReader(myXslText)));

Now I need to apply xslTransformer on myNode. Can anyone show me a code example, which does that? What I've seen so far only works with input and output files.

2 Answers 2

6

Here is an example how to do this, taken from the MSDN documentation:

// Load an XPathDocument.
XPathDocument doc = new XPathDocument("books.xml");

// Locate the node fragment.
XPathNavigator nav = doc.CreateNavigator();
XPathNavigator myBook = nav.SelectSingleNode("descendant::book[@ISBN = '0-201-63361-2']");

// Create a new object with just the node fragment.
XmlReader reader = myBook.ReadSubtree();
reader.MoveToContent();

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("single.xsl");

// Transform the node fragment.
xslt.Transform(reader, XmlWriter.Create(Console.Out, xslt.OutputSettings));

For more information see: http://technet.microsoft.com/en-us/library/wkx4fcc4.aspx

Do note:

When you transform data contained in an XmlDocument or XPathDocument object the XSLT transformations apply to a document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create a separate object containing just the node fragment, and pass that object to the Transform method.

This is why applying the transformation on a node of a document may cause unexpected and unwanted results -- for example the transformation can access other nodes, that aren't in the provided node's subtree -- such as siblings or ancestors.

This is why I strongly recommend not to simply call Transform() on any node (other than a document-node).

Sign up to request clarification or add additional context in comments.

2 Comments

Please note that my answer did not say that you can use the node within the document without consequence. All I said was that you can pass a node into the Transform method. The link I included has guidance on it including your full quote above regarding affecting the document. I'm upvoting your answer for being clearer on this
@KAJ: Yes, thank you, and thank you for editing your answer, too.
3

To apply the transformer you use the Transform method which works on anything that implements IXPathNavigable which includes XmlNode. See http://technet.microsoft.com/en-us/library/ms163430(v=vs.100).aspx for information.

If this isn't what you require can you update the question to describe what you want as output.

EDIT: please note the guidance in the accepted answer (and in the page in the link above) that if you pass a node in that is part of a document it can affect the whole document. Therefore, it is better not to apply a transformation to a node inside an XML document.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.