Introduction
lua-xpath is a simple provides a very Lua library that implements XPath technology.
It enables a Lua program to fetch parts of an XML using xpath expressions.
lua-xpath is based on LuaExpat and on Lua 5.1.
Usage
require "luaxpath" local lom = require "lxp.lom" xpath.selectNodes(lom.parse(xmlString),xpathExpression)
Examples
require "luaxpath"
local lom = require "lxp.lom"
local xmlTest =
[[
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<element id="1" name="element1">text of the first element</element>
<element id="2" name="element2">
<subelement>text of the second element</subelement>
</element>
</root>
]]
-- get all elements
xpath.selectNodes(lom.parse(xmlTest),'//element')
-- get the subelement text
xpath.selectNodes(lom.parse(xmlTest),'/root/element/subelement/text()')
-- get the first element
xpath.selectNodes(lom.parse(xmlTest),'/root/element[@id="1"]')