Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert XML to XML Using XLST

Posted on 2004-04-22
10
Medium Priority
?
900 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 120 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

618 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