When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : XML


Question:

What is the easiest way of extracting certain data from an XML document?


[Print this FAQ]

Answer: This answer comes from Jason Connell

XSL is a great way of displaying XML data, but extracting the right data to display uses something called XPath. The best way to describe XPath is that it extracts data from an XML document in a "filepath" like way. Before I explain, let's define an XML document.

<?xml-stylesheet type="text/xsl" href="Movies.xsl"?>
<MOVIES>
   <MOVIE>
      <TITLE>Pirates of Silicon Valley</TITLE>
      <ACTORS>
         <ACTOR>
            <NAME>Noah Wiley</NAME>
            <CHARACTER>Steve Jobs</CHARACTER>
         </ACTOR>
         <ACTOR>
            <NAME>Anthony Michael Hall</NAME>
            <CHARACTER>Bill Gates</CHARACTER>
         </ACTOR>
      </ACTORS>
      <RATING STARS="10" OUTOF="10" />
   </MOVIE>
   <MOVIE>
      <TITLE>The Matrix</TITLE>
      <ACTORS>
         <ACTOR>
            <NAME>Keanu Reeves</NAME>
            <CHARACTER>Neo</CHARACTER>
         </ACTOR>
         <ACTOR>
            <NAME>Laurence Fishburne</NAME>
            <CHARACTER>Morpheus</CHARACTER>
         </ACTOR>
      </ACTORS>
      <RATING STARS="11" OUTOF="12" />
   </MOVIE>
</MOVIES>

There is my XML document. As you can see, it goes many levels down. My document root is MOVIES, under that is MOVIE, then TITLE and parallel with
TITLE is ACTORS and RATING. Under ACTORS we have a few actors that starred in that movie. Also, in my first line, I define the XSL Style sheet that
will be used to display a nice looking format of this XML document. We will take a look at that file later. However, for right now, let's look at XPath!

XPath was design to address parts of an XML document. Now is a good time to explain how it is like a file path. When you open Windows Explorer and click on the C drive, you notice that the folders under it are displayed in a tree like fashion. This is because there is an hierarchy regarding Drives and Folders and Files. A drive contains Folders and Files, and a Folder contains Files. If you look at my XML document again, you will notice this same behavior. The document is a list of single movies. A movie contains a title, a list of actors, and a rating. An actor contains a name of the actor, and what character they played. Pretty straightforward? Moving on... as you are probably aware, Windows has a method of telling the File
System (the system of drives, folders, and files) exactly where it wants to get data from. This notation is

<DRIVE>:\<DIRECTORY>\<FILENAME>

XPath gives XML users a similar way of accessing data inside their document.

I will show you an example using our MOVIES document.

MOVIES/MOVIE/ACTORS

Since ACTORS is a list of actors, and since XPath picks the FIRST match in a document, this is what the XPath is looking at now:

<ACTOR>
   <NAME>Noah Wiley</NAME>
   <CHARACTER>Steve Jobs</CHARACTER>
</ACTOR>
<ACTOR>
   <NAME>Anthony Michael Hall</NAME>
   <CHARACTER>Bill Gates</CHARACTER>
</ACTOR>

Let's look at the XSL file now.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:template match="/">
      <html>
         <body>
            <table width="100%">
               <xsl:for-each select="MOVIES/MOVIE">
                  <tr>
                     <td>
                        <xsl:value-of select="TITLE" />
                        <span style="padding-left:15px;">
                           My Rating:
                           <font color="red">
                              <xsl:value-of select="RATING/@STARS" />
                               out of
                              <xsl:value-of select="RATING/@OUTOF" />
                           </font>
                        </span>
                        <ul>
                           <xsl:for-each select="ACTORS/ACTOR">
                              <li>
                                 <xsl:value-of select="NAME" />
                                 <ul><li>Played <xsl:value-of select="CHARACTER" /></li></ul>
                              </li>
                           </xsl:for-each>
                        </ul>
                     </td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

If you haven't used XSL, here is a great tutorial on w3schools.com - http://www.w3schools.com/xsl/default.asp. For some articles showing XSL in action, see Using XSL Stylesheets to Translate XML into HTML and
XML and XSL with ASP.

Here's some explanation of some important lines that make use of XPath.

<xsl:template match="/">

This line is like selecting your "Drive" in a file system. You would type C:\ in the DOS window then dir *. This lists all files and folders under that drive. This is the XPath equivalent. Let's look at another line.

<xsl:for-each select="MOVIES/MOVIE">

This line selects and loops through every movie in the document, or database as XML documents have been known to be called, but I'll continue to refer to them as documents.

<xsl:value-of select="TITLE" />

Since this line is inside an xsl:for-each element, and the for-each refers to a MOVIE, we only need to specify TITLE here.

<xsl:value-of select="RATING/@STARS" />

This line selects, for a MOVIE, the attribute of STARS (designated with the @) under the element RATING.

That about covers the most basic usage of XPath. The other for-each loop is a nested loop of ACTORS that are in each movie. There is nothing new about these lines, only that the loop is nested inside another for-each loop, which is the for-each MOVIES/MOVIE loop. I hope this helps with your XPath needs. If you feel the need to know everything about XPath (remember this is hardly even scrubbing the surface), you can find the "Recommendation" at http://www.w3.org/TR/xpath and you can "Google" a tutorial or two I'm sure.

Enjoy!


FAQ posted by Scott Mitchell at 2/18/2003 10:23:19 PM to the XML category. This FAQ has been viewed 54,493 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM



ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article