Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain xpath and xquery in simple terms

Tags:

xpath

xquery

I am new to programming. I know what XML is. Can anyone please explain in simple terms what xpath and xquery do Where are they used?

like image 264
user421814 Avatar asked Sep 07 '10 07:09

user421814


People also ask

What is XPath and XQuery?

Definition. XPath (XML path language) and XQuery (XML query language) are query languages defined by the W3C (World Wide Web Consortium) for querying XML documents. XPath is a language based on path expressions that allows the selection of parts of a given XML document.

What is XQuery explain?

XQuery (XML Query) is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats (JSON, binary, etc.).

What is XQuery xhtml explain?

XQuery is a functional language which is responsible for finding and extracting elements and attributes from XML documents. It can be used for following things: To extract information to use in a web service. To generates summary reports. To transform XML data to XHTML.

What is the meaning of XPath?

XPath stands for XML Path Language. It uses a non-XML syntax to provide a flexible way of addressing (pointing to) different parts of an XML document. It can also be used to test addressed nodes within a document to determine whether they match a pattern or not.


1 Answers

XPath is a way of locating specific elements in an XML tree.

For instance, given the following structure:

<myfarm>
  <animal type="dog">
    <name>Fido</name>
    <color>Black</color>
  </animal>
  <animal type="cat">
    <name>Mitsy</name>
    <color>Orange</color>
  </animal>
</myfarm>

XPath allows you to traverse the structure, such as:

/myfarm/animal[@type="dog"]/name/text()

which would give you "Fido"

XQuery is an XML query language that makes use of XPath to query XML structures. However it also allows for functions to be defined and called, as well as complex querying of data structures using FLWOR expressions. FLWOR allows for join functionality between data sets defined in XML. FLWOR article from wikipedia

Sample XQuery (using some XPath) is:

declare function local:toggle-boolean($b as xs:string) 
as xs:string 
{
    if ($b = "Yes") then "true"
    else if ($b = "No") then "false"
    else if ($b = "true") then "Yes"
    else if ($b = "false") then "No"
    else "[ERROR] @ local:toggle-boolean"
};

<ResultXML>
    <ChangeTrue>{ local:toggle-boolean(doc("file.xml")/article[@id="1"]/text()) }</ChangeTrue>
    <ChangeNo>{ local:toggle-boolean(doc("file.xml")/article[@id="2"]/text()) }</ChangeNo>
</ResultXML>
like image 162
John Nickerson Avatar answered Oct 14 '22 09:10

John Nickerson