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?
2 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>

- 203
- 1
- 7
-
2+1 useful explanation and examples. However 2 corrections: 1) "in an XML DOM" should say "in an XML tree". DOM is a specific interface that is not necessary for XPath. 2) "XQuery is an extension of XPath" - I would say, "XQuery is an XML document query language that uses XPath". Much as the C language uses arithmetic operators, but C is much more than "an extension of" arithmetic operators. – LarsH Sep 13 '10 at 17:00
-
Thanks for the feedback. Clarified my post. – John Nickerson Sep 16 '10 at 13:32
-
@LarsH: It is worth noting that XQuery actually is an extenstion (or superset) of XPath. Any valid XPath expression is also a valid XQuery expression. – Tomas Apr 10 '12 at 14:54
-
@Tomas: I agree with your last sentence, and with "superset". But see my comment about C (or C++ if you like). Would you say C is an "extension" of arithmetic expressions? That term doesn't seem appropriate to me. Would you say that your body is an "extension" of your right hand? – LarsH Apr 10 '12 at 15:06
XPath is a simple query language which serves to search in XML DOM. I think that it can be compared to SQL Select statements with databases. XPath can evaluate many programs which work with XML and has a mass usage. I recommend u to learn it.
XQuery is much more powerful and complicated it also offers many options how to transform result, it offers cycles etc. But also it is query language. It is also used as query language into XML databases. I think that this language has only specific usage and probably is not necessary to know it, in the beginning there will be enough if u know that it exists and what it can
There is simple explanation I hope that it is enough and understandable

- 6,734
- 4
- 38
- 58