CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> .NET / C# >> C# >> Data & I/O >> XML


XSLT Tutorial
Transforming XML into various formats
Rating:

sirgilligan (view profile)
July 30, 2004

Environment:  .NET C#

Go to page: 1  2  3  4  5  Next

XSLT Tutorial

This is a beginner's tutorial on XSLT and XML.


(continued)




Some knowledge of XML, XSLT, and XPath is required, so read some tutorials if necessary.

Selecting a structure for your data in XML is completely arbitrary. You can represent the same data in several different ways. Below is XML that represents the same data four different ways. The XML represents a Census record. A Census record has a country, a year, a small size, and a large size. This information can be represented by elements or elements and attributes.

TYPE1 uses attributes and makes a very small footprint.

TYPE2 uses only elements and because of formatting for readability it uses a little more screen real estate.

TYPE3 and TYPE4 use a combination of elements and attributes.

<?xml version="1.0" encoding="utf-8" ?>
<STUFF>
   <TYPE1>
      <CENSUS COUNTRY="USA" YEAR="1930">
         <PAGE SIZE="SMALL">17x11</PAGE>
         <PAGE SIZE="LARGE">27x19</PAGE>
      </CENSUS>

      <CENSUS COUNTRY="USA" YEAR="1880">
         <PAGE SIZE="SMALL">17x11</PAGE>
         <PAGE SIZE="LARGE">19x25</PAGE>
      </CENSUS>

      <CENSUS COUNTRY="UK" YEAR="1871">
         <PAGE SIZE="SMALL">9.5x15</PAGE>
         <PAGE SIZE="LARGE">9.5x15</PAGE>
      </CENSUS>

      <CENSUS COUNTRY="UK" YEAR="1891">
         <PAGE SIZE="SMALL">11x16</PAGE>
         <PAGE SIZE="LARGE">11x16</PAGE>
      </CENSUS>
   </TYPE1>

   <!-- **************************************************** -->

   <TYPE2>
      <CENSUS>
         <COUNTRY>USA</COUNTRY>
         <YEAR>1930</YEAR>
         <PAGE>
            <SIZE>
               <SMALL>17x11</SMALL>
               <LARGE>27x19</LARGE>
            </SIZE>
         </PAGE>
      </CENSUS>

      <CENSUS>
         <COUNTRY>USA</COUNTRY>
         <YEAR>1880</YEAR>
         <PAGE>
            <SIZE>
               <SMALL>17x11</SMALL>
               <LARGE>19x25</LARGE>
            </SIZE>
         </PAGE>
      </CENSUS>

      <CENSUS>
         <COUNTRY>UK</COUNTRY>
         <YEAR>1871</YEAR>
         <PAGE>
            <SIZE>
               <SMALL>9.5x15</SMALL>
               <LARGE>9.5x15</LARGE>
            </SIZE>
         </PAGE>
      </CENSUS>

      <CENSUS>
         <COUNTRY>UK</COUNTRY>
         <YEAR>1891</YEAR>
         <PAGE>
            <SIZE>
               <SMALL>11x16</SMALL>
               <LARGE>11x16</LARGE>
            </SIZE>
         </PAGE>
      </CENSUS>
   </TYPE2>

   <!-- **************************************************** -->

   <TYPE3>
      <CENSUS>
         <USA YEAR="1930">
            <PAGE SIZE="SMALL">17x11</PAGE>
            <PAGE SIZE="LARGE">27x19</PAGE>
         </USA>

         <USA YEAR="1880">
            <PAGE SIZE="SMALL">17x11</PAGE>
            <PAGE SIZE="LARGE">19x25</PAGE>
         </USA>

         <UK YEAR="1871">
            <PAGE SIZE="SMALL">9.5x15</PAGE>
            <PAGE SIZE="LARGE">9.5x15</PAGE>
         </UK>

         <UK YEAR="1891">
            <PAGE SIZE="SMALL">11x16</PAGE>
            <PAGE SIZE="LARGE">11x16</PAGE>
         </UK>
      </CENSUS>
   </TYPE3>

   <!-- **************************************************** -->

   <TYPE4>
      <CENSUS>
         <COUNTRY>
            USA
            <YEAR>
               1930
               <PAGE>
                  <SIZE TYPE="SMALL">17x11</SIZE>
                  <SIZE TYPE="LARGE">27x19</SIZE>
               </PAGE>
            </YEAR>
            <YEAR>
               1880
               <PAGE>
                  <SIZE TYPE="SMALL">17x11</SIZE>
                  <SIZE TYPE="LARGE">19x25</SIZE>
               </PAGE>
            </YEAR>
         </COUNTRY>
         <COUNTRY>
            UK
            <YEAR>
               1871
               <PAGE>
                  <SIZE TYPE="SMALL">9.5x15</SIZE>
                  <SIZE TYPE="LARGE">9.5x15</SIZE>
               </PAGE>
            </YEAR>
            <YEAR>
               1891
               <PAGE>
                  <SIZE TYPE="SMALL">11x16</SIZE>
                  <SIZE TYPE="LARGE">11x16</SIZE>
               </PAGE>
            </YEAR>
         </COUNTRY>

      </CENSUS>
   </TYPE4>
</STUFF>

Often, an XML document needs to be converted to a new structure. That is where XSLT comes in. There are lots of good tutorials on XSLT. I found that there weren't very many examples that covered more than a few aspects of XSLT. Thus, I decided to write an XSLT for each type in my XML document. So, I wrote some XSLT to convert each type into all of the other types. Some things I had to overcome were converting attributes into elements, element values into attributes, selecting nodes back up the tree (the parent in my case), stripping off white space, and adding white space.

About the Author
MS/CS. .NET, J2EE, C++, Tibco Rendezvous, MFC, OS X, XML, XSLT. Interests: 2D/3D/ND Computer Graphics, Messaging, Webservices, Software Process. Real name: Geoffrey Slinker http://home.att.net/~geoffrey.slinker/agile.html

Go to page: 1  2  3  4  5  Next

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
how to create XSL file using xmlspy? - shekhar_gpt (03/18/2005)
XSLT Example, not Tutorial - Alok Govil (08/02/2004)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)