Solved

Convert XML to XML Using XLST

Posted on 2004-04-22
10
890 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factory design pattern vs abstract factoy design pattern 2 97
Adding a hidden div to each row in a table 6 61
Re-position sub-options beneath the TAB 7 74
Animated .jpg? 13 57
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now