XPath Reference (version 2.05)

Contents


Browse through the topics in the contents list to the right.

xpath

Selects nodes and attributes from a given standard xpath expression.

xpath(doc, step, set)

Parameters

Returns The raw XML source of the selection. If the content was set to be modified the previous value will be returned.
doc A variable created by xpath_load().
step A valid XPath 2.0 expression.
set (optional) Text content to replace the current selection.

Remarks

To create nodes use a ±1 indexed predicate. Only the following functions are supported: text(), remove(), position(), last(), and count().

Function Description Example
text() Returns the text content of the selection. version := xpath(feed, "/rss/@version/text()")
remove() Removes the selection. new := xpath(feed, "/rss/channel/item[1]/remove()")
position() Returns the position of the selection in the stack. top := xpath(feed, "/rss/channel/item[position()<=3]")
last() Returns the last object in a selection. oldest := xpath(feed, "/rss/channel/item/last()/pubDate")
count() Returns the total number of elements in a selection. entries := xpath(feed, "/rss/channel/item/count()")

Examples

#Include xpath.ahk ; include functions (only required if this is not in your stdlib)

; parsing documents:
xml := xpath_load("books.xml") ; load an XML document
titles := xpath(xml, "/bookstore/books[price>35]/title/text()") ; get book titles with a cost greater than 35
; combining functions and using variables:
firstShow := xpath(xml := xpath_load("listings.xml"), "/tv/programme[@starts>=" . A_Now . "]/title[1]/text()")
; creating documents:
xpath(feed, "/rss[+1]/@version", "2.0") ; create 'rss' node with attribute 'version'
xpath(feed, "/rss/channel[+1]/title[+1]", "second") ; rss > channel > title
xpath(feed, "/rss/channel/title[-1]", "first") ; '-1' puts this node before the last
xpath_save("\Inetpub\wwwroot\test.xml", feed) ; write XML

xpath_load

Loads an XML document.

xpath_load(doc, src)

Parameters

Returns False if there was an error in loading the document, true otherwise.
doc A reference to the loaded XML file as a variable.
src The document to load, this can be a filename or a string.

Remarks

The variable passed as the first parameter should be used as an identifier for the XML document in other functions.

xpath_save

Saves an XML document.

xpath_save(doc, src)

Parameters

Returns False if there was an error in saving the document, true otherwise.
doc A variable created by xpath_load().
src A path to a file where the XML document should be saved; if the file already exists it will be replaced.
Version 2.03; Copyright © Titan
GNU General Public License 3.0 or higher <http://www.gnu.org/licenses/gpl-3.0.txt>