Version 4 (modified by will.schneider, 6 years ago) (diff)

added warning about complex expressions

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. In order to use xpath you must add the attached xpath.lua file to your Vera Controller. The easiest way to do this is to upload the file under the develop apps section of the interface. Once uploaded the below sample code can be used to prototype your specific needs.

It should be noted that more complex XPath expressions may not work with this module. Specifically, wildcards that are mid-expression may fail to evaluate. Generally, the XPath expression should terminate with a node, text(), value() or wildcard (*).

Sample Code

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