5,696,038 members and growing! (15,093 online)
Email Password   helpLost your password?
Languages » XML » XSLT     Intermediate

Creating a Node Tree Fragment in XSLT Using Internet Explorer

By Mike DiRenzo

This is a technique describing how to create node-tree-fragments in XSLT using C#. Additionally, this aritcle describes how to use C# in XSLT.
C#, XML, XSLT, Windows, .NET, Visual Studio, Dev

Posted: 18 May 2006
Updated: 19 May 2006
Views: 19,625
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
9 votes for this Article.
Popularity: 3.60 Rating: 3.77 out of 5
1 vote, 11.1%
1
1 vote, 11.1%
2
0 votes, 0.0%
3
2 votes, 22.2%
4
5 votes, 55.6%
5

Before transformation

Image A - Before transformation

After Transformation

Image B - After transformation

Introduction

This article describes a means of using C# to create a node-tree-fragment in XSLT.

Overview

The concept derives from embedding well-formed XML inside an existing XML object programmatically, by setting the inner text portion of a given element. I could have used an XML DOM append child node technique, but for the purposes of this example, I chose not to. When you do this, the embedded well-formed XML is treated as a string, not as XML nodes. Image A shows the embedded XML in black color. You cannot expand or collapse any of the embedded XML nodes. Several solutions that I have found required me to use a different XML parser other than what is provided via Microsoft - Xerces comes to mind. In fact, the Microsoft XML parser does not include a native function for converting strings to node-tree-fragments.

So, how can you access the values or the elements? A conversion of the XML string to a node tree fragment needs to take place.

The problem: I needed to embed XML within XML. Afterwards, a transformation from one XML format to another is performed. Embedding XML within XML is a practice not recommended by various XML pundits. However, when the situation cannot be modified such that a best-practices approach can occur, there is not a whole lot one can do. Enter IE XML/XSLT node-tree-fragment creation using the .NET Framework!

Using the code

The real trick is setting up the XSLT file with the appropriate namespaces:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:ms="urn:schemas-microsoft-com:xslt" 
 xmlns:cs="urn:the-xml-files:xslt-csharp"
 exclude-result-prefixes="cs ms">

Once the namespace declarations have been set, adding any number of .NET Framework aware classes is relatively easy. I use the XPathNodeIterator function to convert the well-formed XML string to a node-tree-fragment.

<ms:script language="C#" implements-prefix="cs">
<![CDATA[

XPathNodeIterator parse(String strXML)
{
    System.IO.StringReader rdr = new System.IO.StringReader(strXML);
    XPathDocument doc = new XPathDocument(rdr);
    XPathNavigator nav = doc.CreateNavigator();

    XPathExpression expr; 
    expr = nav.Compile("/"); 

    XPathNodeIterator iterator = nav.Select(expr);

    return iterator;
}

]]>
</ms:script>

Here is a sample XML snippet. Note the <system_codes> element. Contained within, I programmatically embed a well-formed XML string. When the MS XML parser interprets this, all of the less-than and greater-than characters get escaped. The MS XML parser will escape the XML programmatically for you because it is treated as text/string. I could have used an AppendChild method to merge the XML, but then there would be no need for this article!

<request>
 <tkrnum>300005</tkrnum>
 <zertnum>20010003</zertnum>
 <username>DIRENZO</username>
 <result>implemented</result>
 <system_codes>Embedded Well-Formed XML is here - download 
               article source to see it</system_codes>
 </system_codes>
</request>

Here is the XSL code snippet that accesses the Parse function and describes the iteration process:

<xsl:variable name="syscodes" select="cs:parse(system_codes)"/>
<internal_sys_codes>
 <!-- Iterate here-->
 <xsl:for-each select="$syscodes//trresult" >     
  <sys_code>
   <icode><xsl:value-of select="srdcode"/></icode>
   <icomment><xsl:value-of select="comment"/></icomment>
  </sys_code>
 </xsl:for-each>
</internal_sys_codes>

Note the cs: prefix that references the namespace contained in the XSLT header. I put the result of cs:parse into syscodes. Then, the iteration of the new node-tree-fragment contained in syscodes occurs. Through this iteration, I can use the values contained in the originally embedded XML string (now a node-tree-fragment) and reshape the XML.

Points of Interest

The real crux of this technique is that it cannot be performed unless we do a server-side transform. The transform must be done with the .NET Framework, or an Invalid class string exception is thrown. Simply referencing the XSL file within the XML file will not work. See Image B for the results of the transformation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mike DiRenzo


I have lived and worked throughout the globe (Brussels, London, Paris, West Indies, USA) as an IT consultant. My favorite S/W development concentrations are XML/XSLT, C#, Agile Model - Driven Development. I admire two well-renowned authors: Scott Ambler and Sal Mangano. Obviously there are many, many others but these two have been key to my successes over the years. I live and work in Charlotte, NC, USA and run a small S/W consulting practice called Business Automation Systems, Inc. I hold a Bachelors degree in Business with a concentration in Management Information Systems from the University of North Carolina at Charlotte.
Occupation: Web Developer
Location: United States United States

Other popular XML articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralWow I never even thought about thismemberIsraelDC110:14 22 Mar '07  
Generalvb .net - XSLT NamespacememberPatrick.Alex2:52 28 Nov '06  
GeneralRe: vb .net - XSLT NamespacememberMike DiRenzo16:40 21 Dec '06  
Questionxml transform to xml using xslt ???memberphucuong2734:07 21 May '06  
AnswerRe: xml transform to xml using xslt ??? [modified]memberMike DiRenzo3:34 26 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 May 2006
Editor: Smitha Vijayan
Copyright 2006 by Mike DiRenzo
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project