Solved

Convert XML to XML Using XLST

Posted on 2004-04-22
10
891 Views
Last Modified: 2013-11-19
Hopefully, this question is easy enough to justify the 30 points :)
Basically, I need to transform one XML document into another using XSLT in C#.

Assume a very simplistic input XML document:
<Parent>
   <Child>
      <Node1 attribute="Name">Bob</Node1>
      <Node2 attribute="Name">Jim</Node2>
   </Child>
   <Child2>
      <Node22 attribute="Something">Hello World</Node11>
      <Node22 attribute="SomethingElse">Hello Universe</Node22>
  </Child2>
</Parent>


And, I don't care about the output so make something up that will show how to acomplish the various nodes.
Finally, how do I programatically apply an XSLT to an XML Doc in C# and then get access to the transformed XML doc.

Make sense?
0
Comment
Question by:winterminute
  • 4
  • 3
10 Comments
 
LVL 20

Expert Comment

by:Venabili
ID: 10898423
winterminute,

THis question will be better answered in the C# area. Want to move it?

Venabili
Languages Page Editor
0
 
LVL 2

Author Comment

by:winterminute
ID: 10901688
Yes, please move it!  Thanks!
0
 
LVL 8

Expert Comment

by:gregasm
ID: 10902806
//this is assuming we have an XML string for input named incoming_xml...

//first we convert the XML to another XML using a stylesheet...
                        //load the xsl object...
                        XslTransform xslt = new XslTransform();
                        xslt.Load(Server.MapPath(".") + @"\" + "your_stylesheet.xsl");
                  
                        //create the memory stream output objects
                        MemoryStream strm = new MemoryStream();
                        TextWriter textwriter = new StreamWriter(strm);
                        XmlTextWriter writer = new XmlTextWriter(textwriter);

                        //create the nav
                        XmlDocument xdoc = new XmlDocument();
                        xdoc.LoadXml(incoming_xml);

                        xslt.Transform(xdoc, null, writer, null);

                        //now read the stream into a string...
                        strm.Position = 0;
                        TextReader reader = new StreamReader(strm);
                        string xmlNew = reader.ReadToEnd();

//and the xmlNew string contains the transformed XML....
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:winterminute
ID: 10903265
I guess I wasn't specific enough :)
I need to write the actual XSLT used to do the transform.
If you can write a very very basic XLST to transform some basic XML, I think I can take it from there.
0
 
LVL 8

Accepted Solution

by:
gregasm earned 30 total points
ID: 10903936
This is the XML::

<Parent>
      <Child>
            <Node1 name="take this">Bob</Node1>
            <Node1 name="Name">Jim</Node1>
      </Child>
      <Child2>
            <Node22 name="Something">Hello World</Node22>
            <Node22 name="take that">Hello Universe</Node22>
      </Child2>
</Parent>

This is the XSL::

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
            <xsl:element name="People">
                  <xsl:value-of select="//Parent/Child/Node1[@name='take this']"/>
            </xsl:element>
            <xsl:element name="AnotherPerson">
                  <xsl:value-of select="//Parent/Child2/Node22[@name='take that']"/>
            </xsl:element>
      </xsl:template>
</xsl:stylesheet>

This is the output of the transformation::

<People>Bob</People><AnotherPerson>Hello Universe</AnotherPerson>

Hope that helps...
0
 
LVL 2

Author Comment

by:winterminute
ID: 10903962
What is the difference between these two XLST:
Hard coding XML elements vs creating them via XLST?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
          <xsl:element name="People">
               <xsl:value-of select="//Parent/Child/Node1[@name='take this']"/>
          </xsl:element>
     </xsl:template>
</xsl:stylesheet>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
         <People>
               <xsl:value-of select="//Parent/Child/Node1[@name='take this']"/>
          </People>
     </xsl:template>
</xsl:stylesheet>
0
 
LVL 8

Expert Comment

by:gregasm
ID: 10904011
Well, if you use <xsl:element name=Variable1> in code, and Variable1 = "People".. this gives you more control over the XSL output. If you use <People> it is considered "hard coded" in..
0
 
LVL 2

Author Comment

by:winterminute
ID: 10904017
Okay..sound good.  Thanks for your help!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 163
data scientists and AI 17 108
wordappend challenge 8 179
Advice in Xamarin 21 79
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question