Solved

Convert XML to XML Using XLST

Posted on 2004-04-22
10
889 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

16 Experts available now in Live!

Get 1:1 Help Now