top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Xpath Expressions and Functions

0 votes
297 views

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.

posted Dec 5, 2017 by Jon Deck

  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

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.
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

Introduction

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents. Sometimes, the user wants some kind of XML structure instead of the whole XML. In that case, we can use XSLT transformation. This article will give us an idea about this.

Please have a look at the below image of the high-level structure.

XSLT

Let's create a sample console application to see how it works.

  1. Create a console application. Open Visual Studio - File - New Project - Console Application; give it a suitable name, such as - ‘XSLT_XML_Transformation’ and click OK.

    XSLT

 

  1. Let's create a main XML, i.e., input.xml. Right click on ‘Solution Explorer’ - Add - New Item and select XML file; give it a name like input.xml.

    XSLT

    I have created the below XML which contains a note that is the main element and under this, it contains other elements.

XML

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <note>  
  3.     <from>sagar@gmail.com</from>  
  4.     <to>swapnil@gmail.com</to>  
  5.     <cc>pankaj@gmail.com</cc>  
  6.     <bcc>amit@gmail.com</bcc>  
  7.     <heading1>Reminder</heading1>  
  8.     <heading2>Important</heading2>  
  9.     <body>Don't forget me this weekend!</body>      
  10. </note>  

Requirement

We have created  atransform file that generally has only 3 elements and also applied concatenation functionality for headings, separated by commas.

 

 

  1. Now, create an XSLT transform file to match the above requirement. Right-click on ‘Solution Explorer’ - Add - New Item and select XSLT File and give it a name like transform.xslt.

    XSLT

    XSLT code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
  3.     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">  
  4.     <xsl:output method="xml" indent="yes"/>  
  5.     <xsl:template match="/">  
  6.         <Details>  
  7.             <From>  
  8.                 <xsl:value-of select="note/from"/>  
  9.             </From>  
  10.             <To>  
  11.                 <xsl:value-of select="note/to"/>  
  12.             </To>  
  13.             <Heading>  
  14.                 <xsl:value-of select="concat(note/heading1,', ',note/heading2)" />  
  15.             </Heading>  
  16.         </Details>  
  17.     </xsl:template>  
  18. </xsl:stylesheet>  

Some summary of XSLT functions is as below.

    • <xsl:value-of> – The <xsl:value-of> element is used to extract the value of a selected node.
    • Concat – The fn:concat function requires at least two arguments (which can be the empty sequence), and accepts an unlimited number of additional arguments.
    • <xsl:for-each> - The <xsl:for-each> element allows you to do looping in XSLT
    • <xsl:if> - The <xsl:if> element is used to put a conditional test against the content of the XML file
    • <xsl:choose> - The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests
    • format-number() - The format-number() function is used to convert a number into a string.

 

  1. Write the C# code to read the transform and XML files and generate the desired output XML.

    XSLT

 

  1. Result 
    Please check the output result.
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <Details>  
    3.   <From>sagar@gmail.com</From>  
    4.   <To>swapnil@gmail.com</To>  
    5.   <Heading>Reminder, Important</Heading>  
    6. </Details>  

 

Conclusion

    I hope that you got an idea of how to use XSLT.

    READ MORE
    ...