Version 2 (modified by javier_guerra, 15 years ago) (diff)

--

xpath.lua

LuaExpat is installed in the standard MIOS software. This Lua extension lets you create SAX-based XML parsers in Lua. LOM is one of these parsers is included in the LuaExpat package. Given a XML document, LOM creates a nested table structure that replicates the XML structure.

LuaXPath is a utility function that lets you use XPath expressions to query a LOM object.

require "xpath"
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>
]]

local lomobj = lom.parse(xmlTest)

-- get all elements
xpath.selectNodes(lomobj,'//element')
-- get the subelement text
xpath.selectNodes(lomobj,'/root/element/subelement/text()')
-- get the first element
xpath.selectNodes(lomobj,'/root/element[@id="1"]')

Attachments

  • xpath.lua (4.1 KB) - added by will.schneider 6 years ago. XPath Module Required on Vera Controller