Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

XPath Tutorial
XPath Introduction
XPath Nodes
XPath Syntax
XPath Axes
XPath Operators
XPath Examples
XPath Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


XPath Examples

Previous Next



XPath Example



Example: Training Log Sample XML Document

<?xml version='1.0'?> <DOCTYPE training system'etml.dtd'> <training>

<session date='11/10/05' type='running' heartrate='158'>
<duration units="minutes">45 </duration>
<distance units="miles">4.5 </distance>
<location>west Park </location>
<comments>little wind through out</comment>
</session>

<session date='11/11/05' type='cycling' heartrate='153'>
<duration units="hours">2.5 </duration>
<distance units="miles">37 </distance>
<location> Trace Parkway </location>
<comments>afternoon run</comment>
</session>

<session date='11/24/05' type='cycling' heartrate='153'>
<duration units="hours">1.5 </duration>
<distance units="miles">8.5 </distance>
<location> Warner Park </location>
<comments>afternoon run</comment>
</session>
</training>

Description:

The most basic of XPath patterns is the pattern that references the current node, which consists of a period sign:

.

A period will obtain the current node.when you’re traversing a document tree. Thus current node pattern is therefore a relative pattern because it makes sense only in the context of a tree of data. As a contrast to the current pattern, which is relative, consider the pattern that is used to select the root node of a document. This pattern is called as root pattern and consists of a single forward slash:

/

If you are using a single forward slash in an expression for the training log sample document, it would refer to the trainlog element because this element is the root element of the document. Because the root pattern directly references a specific location in a document (the root node), it is also considered an absolute pattern. The root pattern is extremely important to XPath because it represents the starting point of the document’s node tree.

If there are child nodes there must also be parent nodes. To access a parent node, you must use two periods:

..

As an example, if the current context is one of the distance elements then in the training log document, the .. parent pattern will reference the parent of the node, which is a session element. You can put patterns together to get more interesting results. e.g. to address a sibling node, you must first go to the parent and then references the sibling as a child. In other words, you use the parent pattern (..) followed by a forward slash (/) followed by the sibling name, like this:

../duration

This pattern assumes that the context is one of the child elements of the session element (other than duration). Assuming this context, the ../duration pattern will reference the duration element as a sibling node.

Thus far I’ve focused on referencing individual nodes. However, it’s also possible to select multiple nodes. For example, you can select all of the child nodes (descendants) of a given node using the double slash pattern:

//

As an example, if the context is one of the session elements in the training log document.e.g. you can select all of its child nodes by using double slashes. This results in the duration. , distance , location , and comments elements being selected.

Another way to select multiple nodes is to use the wildcard pattern, which is an asterisk:

*

The wildcard pattern selects all of the nodes in a given context. So, if the context was a session element and you used the pattern */distance, all of the distance elements in the document would be selected. This occurs because the wildcard pattern first results in all of the sibling session elements being selected, after which the selection is limited to the child distance elements.

To summarize, following are the primary building blocks used to reference nodes in XPath:

  • Current node—.
  • Root node—/
  • Parent node—..
  • Child node—Child
  • Sibling node—/Sibling
  • All child nodes—//
  • All nodes—*
Referencing Subsets and Attributes

Elements are not only important pieces of information in XML documents; it’s also important to be able to references attributes. Fortunately, XPath makes it quite easy to reference attributes by using the “at” symbol:

@

The at symbol is used to reference attributes by preceding an attribute name:

*/distance/@units

This code selects all of the units attributes for distance elements in the training log document, assuming that the context is one of the session elements. As you can see, attributes fit right into the path notation used by XPath and are referenced in the same manner as elements, with the addition of the at (@) symbol.

One other important feature of XPath expressions is support for the selection of subsets of nodes. You select a subset by appending square brackets ([]) to the end of a pattern and then placing an expression within the brackets that defines the subset. As an example, consider the following pattern that selects all the session elements in the training log document:

*/session

It’s possible that you might want to limit the session elements to a certain type of training session, such as running. To do this, you add square brackets onto the pattern, and you create an expression that checks to see if the session type is set to running:

*/session[@type=’running’]

This pattern results in selecting only the session elements whose type attribute is set to running. Notice that an at symbol (@) is used in front of the attribute name (type) to indicate that it is an attribute. You can also address elements by index, as the following expression demonstrates:

/session[1]

This expression selects the first session element in the document.




Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords: XPath Examples, xpath query examples, xslt example, xslt examples, xpath example, xsl example, xsl examples, xquery examples, xquery example, xpath vb net, xpath starts with, asp net xpath, xml xslt example, java xpath example, xml xsl example, vb net examples, asp net examples, xsl fo examples, xpath case insensitive, xml xpath example, xsl fo example, xpath query example, xalan example, java xslt example, xpath tutorial, xpath sample, xml examples, xpath syntax, xpath namespace, xpath reference, xpath expressions, xpath c# , xpath namespaces, xpath xml, selectsinglenode xpath, xpath functions, xpath query, xpath attribute


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.