top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Benefits of XPath

0 votes
346 views

Benefits of XPath

XPath is designed for XML documents. It provides a single syntax that you can use for queries, addressing, and patterns. XPath is concise, simple, and powerful.

XPath has many benefits:

  • Syntax is simple for the simple and common cases.
  • Any path that can occur in an XML document and any set of conditions for the nodes in the path can be specified.
  • Any node in an XML document can be uniquely identified.

XPath is designed to be used in many contexts. It is applicable to providing links to nodes, for searching repositories, and for many other applications.

XML Document in XPath

In XPath, an XML document a viewed conceptually as a tree in which each part of the document is represented as a node as shown in figure below:

XPath has seven types of nodes. They are:

  • Root: The XPath tree has a single root node, which contains all other nodes in the tree.
  • Element: Every element in a document has a corresponding element node that appears in the tree under the root node. Within an element node appear all of the other types of nodes that corresponding to the element’s content. Element nodes may have a unique identifier associated with them that is used to reference the node with XPath.
  • Attribute: Each element node has an associated set of attribute nodes, the element is the parent of each of these attribute nodes, however, an attribute node is not a child of its parent element.
  • Text: Character data is grouped into text nodes. Characters inside comments, processing instructions and attribute values do not produce text nodes. The text node has a parent node and it may the child node too.
  • Comment: There is a comment node for every comment, except for any comment that occurs within the documents type declaration. The comment node has a parent node and it may be the child node too.
  • Processing instruction: There is a processing instruction node for every processing instruction, except for any processing instruction that occurs within the document type declaration. The processing instruction node has a parent node and it may be the child node too.
  • Namespace: Each element has an associated set of namespace nodes. Although the namespace node has a parent node, the namespace node is not considered a child of its parent node because they are not contained in a parent node, but are used to provide descriptive information about their parent node.
posted Dec 2, 2017 by Frank Lee

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

XML and XPath Program: Xpath is called for finding the information within the document.


 


What is XPath?

  1. XPath is a syntax to define the parts of XML document.
  2. XPath uses the path expressions to navigate in XML documents.
  3. XPath contains a library of the standard functions.
  4. XPath is a major element in XSLT.
  5. XPath is also used in XQuery, XPointer and XLink.
  6. XPath is a W3C recommendation. 

Softwares

 

XML copy Editor, XEditor. 

 

Program  

  1. <book>  
  2.     <title>XML</title>  
  3.     <author>Erik T. Ray</author>  
  4.     <year>2003</year>  
  5.     <price>300</price>  
  6. </book>  

Xpath

 

/book 

 

output
 

XML

Erik T. Ray

2003

300

 

Xpath

 

/book/author

 

output

 

Erik T. Ray

READ MORE

XPath Expressions

XPath Expressions are statements that can extract useful information from the XPath tree. Instead of just finding nodes, one can count them, add up numeric values, compare strings, and more. They are much like statements in a functional programming language. Every XPath expression evaluates to a single value.

There are four types of expressions in XPath. They are:

  • Node-set: A node-set is an unordered group of nodes from the input document that match an expression’s criteria.
  • Boolean: A Boolean has one of two values: true of false. XSLT allows any kind of data to be transformed into a Boolean. This is often done implicity when a string or a number or a node-set used where a Boolean is expected.
  • Number: XPath numbers are numeric values useful for counting nodes and performing simple arithmetic. The numbers such as 43 or -7000 that look like integers are stored as doubles. Non-number values, such as strings and Booleans, are converted to numbers automatically as necessary.
  • String: A string is a sequence of zero or more Unicode characters. Other data types can be converted to strings using the string() function.

XPath Funcitons

XPath defines various functions required for XPath 2.0, XQuery 1.0 and XSLT 2.0. The different functions and Accessor, AnyURI, Node, Error and Trace, Sequence, Context, Boolean, Duration/Date/Time, String, QName and Numeric.

XML Path Language (XPath) functions can be used to refine XPath queries and enhance the programming power and flexibility of XPath. Each function in the function library is specified using a function prototype that provides the return type, function name, and argument type. If an argument type is followed by a question mark, the argument is optional; otherwise, the argument is required. Function names are case-sensitive.

The default prefix for the function namespace is fn.

READ MORE

Introduction to XPath

XPath can be thought of as a query language such as SQL. However, rather than extracting information from a database, it extracts information from an XML document. XPath is a language for retrieving from a database, it extracts information from an XML document. XPath is a language for retrieving information from a XML document. XPath is used to navigate through elements and attributes in an XML document. Thus, XPath allows identifying parts of an XML document.

XPath provides a common syntax as shown in figure:-

 

  • XSLT: XSLT is a language for transforming XML documents into XML, HTML, or text.
  • XQuery: XQuery builds on XPath and is a language for extracting from XML documents.

Benefits of XPath

XPath is desingned for XML documents. It provides a single syntax that you can use for queries, addressing, and patterns. XPath is concise, simple, and powerful.

XPath has many benefits:

  • Syntax is simple for the simple and common cases
  • Any path that can occur in an XML document and any set of conditions for the nodes in the path can be specified.
  • Any node in an XML document can be uniquely identified

XPath is designed to be used in many contexts. It is applicable to providing links to nodes, for searching repositories, and for many other applications.

READ MORE

This article describes how to transform the XML output of SQL query statements using FOR XML AUTO to HTML using XSLT.

Normally, we execute the query using ExecuteNonQuery statement and fill the output into a data table. We can access the data in the data table by giving column name and row number in the looping method. This makes us work on extra coding and takes a lot of time to get the correct data from executed query. So, I moved to XML because, in XML, we can fetch the values easily and quickly without writing too much code.     

FOR XML Clause in SQL

Basically, we have four types of FOR XML clause in SQL.

  • FOR XML RAW
  • FOR XML AUTO
  • FOR XML EXPLICIT
  • FOR XML PATH

Each of the above types satisfies its own need. When you want full control over the produced XML, you use FOR XML EXPLICIT, but it is difficult to understand, read, or maintain. FOR XML AUTO produces the most readable SELECT statement. The RAW option is rarely used and therefore not discussed. The PATH option allows you to mix attributes and elements easier. Here, I gona use FOR XML AUTO.

Table Name - Contact_Details

Contact_IDContact_NumberContact_NameContact_Location
001088888888MohanChennnai
002099999999AbilashBangalore


If we want the output as in XML format, we need to add the FOR XML AUTO statement.

 

  1. SELECT * FROM Contact_Details FOR XML AUTO  

This query gives the output like,

  1. < Contact_Details Contact_ID=’001’ Contact_Number=’ 088888888’ Contact_Name=’Mohan’ Contact_Location=’Chennai’/>  
  2. < Contact_Details Contact_ID=’002’ Contact_Number=’ 099999999’ Contact_Name=’Abilash’ Contact_Location=’Bangalore’/>  

The XML which we got in output is not in user-friendly format. We need each field as an attribute so, that we can access the values using XPATH. To achieve that, we need to add the ELEMENTS parameter with the query.

 

  1. SELECT * FROM Contact_Details FOR XML AUTO, ELEMENTS  

This query gives the output like,

  1. < Contact_Details>  
  2.     <Contact_ID>001</Contact_ID>  
  3.     <Contact_Number>088888888</Contact_Number>  
  4.     <Contact_Name>Mohan</Contact_Name>  
  5.     <Contact_Location>Chennai</Contact_Location>  
  6. </ Contact_Details>  
  7. < Contact_Details>  
  8.     <Contact_ID>002</Contact_ID>  
  9.     <Contact_Number>099999999</Contact_Number>  
  10.     <Contact_Name>Abilash</Contact_Name>  
  11.     <Contact_Location>Bangalore</Contact_Location>  
  12. </ Contact_Details>  

So, this is the exact XML format we need.

Now, we are going to create XML DOCUMENT and write this output XML into that XML document in coding. To read the query in XML format, we need to execute the query using ExecuteXmlReader() function. 

  1. XmlReader XMLReader = new XmlReader();  
  2. XmlDocument XmlDoc = new XmlDocument();  
  3. SqlCommand Cmd = new SqlCommand(Query, Conn);  
  4. XMLReader = cmd.ExecuteXmlReader();  
  5. XPathDocument xPathDoc = new XPathDocument(XMLReader);  
  6. XPathNavigator xPathNavi = xPathDoc.CreateNavigator();  
  7. XmlDoc.LoadXml(XpathNavi.OuterXml);  

Now, we got the full structured XML Document. Using this, we will transform the data into HTML format using XSLT.

Before transforming, we need to create one XSLT file to assign the values from XMLDocument to HTML format.

Contact_Details.xslt

  1. <?xml version=”1.0” encoding=”utf-8”?>  
  2. <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Tranform”>  
  3.     <xsl:output method=”xml” indent=”yes” omit-xml-declaration=”yes”>  
  4.         <xsl:template match=”/*”>  
  5.             <table>  
  6.                 <tr>// Column heading  
  7.                     <td>ContactID</td>  
  8.                     <td>ContactNumber</td>  
  9.                     <td>ContName</td>  
  10.                     <td>ContactLocation</td>  
  11.                 </tr>  
  12.                 <xsl:for-each select=”/ Contact_Details”>  
  13.                     <tr>// inserting XML data into HTML table  
  14.                         <td>  
  15.                             <xsl:value-of select=”/Contact_details/Contact_ID” </td> // using XPath we can access the value from XML Document  
  16.                                 <td>  
  17.                                     <xsl:value-of select=”/Contact_details/Contact_Number” </td>  
  18.                                 <td>  
  19.                               <xsl:value-of select=”/Contact_details/Contact_Name” </td>  
  20.                           <td>  
  21.                        <xsl:value-of select=”/Contact_details/Contact_Location” </td>  
  22.                     </tr>  
  23.                 </xsl:for-each>  
  24.             </table>  
  25.         </xsl:template>  
  26. </xsl:stylesheet>  

Now, we have XML Document and XSLT file. In the next step, we are going to transform the XML Document into HTML using XSLT. To do this, we pass the XMLDocument and XSLT file to separate functions and get fully formed HTML strings in return.

  1. Public static string TransformXMLTOHTML(XmlDocument XMLDoc, String XSLTFilename) {  
  2.     StringWriter HTMLString = new StringWriter();  
  3.     XslCompiledTransform Xmltransform = new XslCompiledTransform();  
  4.     XmlTransform.Load(XSLTFilename); // Load XSLT File  
  5.     Xmltranform.Transform(XMLDoc, null, Result); // transform XML into HTML  
  6.     Return HTMLString.ToString();  
  7. }  
READ MORE
...