Solved

Convert XML to XML Using XLST

Posted on 2004-04-22
10
896 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 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

691 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