It is better to work with luaxml-domobject
library, as it provides higher level interface and fixes one fatal isssue that simpleTreeHandler
has - it doesn't keep the correct element order in mixed order XML. It makes it suitable for HTML, for example. I've learned this hard way when I used it for the odsfile package. It cannot keep correct order of formatting elements in table cells. It is the original part of LuaXML library and while it seems easiest to use, when you bounce into this issue, it is something that cannot be fixed. It is best to avoid it. Really.
Anyway, I will show solution for both simpleTreeHandler
and DOM Object
.
\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
xml = require('luaxml-mod-xml')
handler = require('luaxml-mod-handler')
\end{luacode*}
\begin{document}
\begin{luacode*}
sample = [[
<DATA>
<TITLE>Quantum vacuum ç under mixed boundary conditions: the case for curved spacetime</TITLE>
<NAME="name" />
<VERSION="1" />
<VARIABLES>
<TEST>
<ELEMENT attr="A1">123.000000001</ELEMENT>
<ELEMENT attr="A2">789.000000001</ELEMENT>
</TEST>
</VARIABLES>
</DATA>]]
treehandler = handler.simpleTreeHandler()
x = xml.xmlParser(treehandler)
x:parse(sample)
tex.sprint(treehandler.root["DATA"]["TITLE"])
for _, element in ipairs(treehandler.root["DATA"]["VARIABLES"]["TEST"]["ELEMENT"]) do
if element._attr["attr"] == "A2" then
tex.sprint(element)
end
end
\end{luacode*}
\end{document}
reehandler.root["DATA"]["VARIABLES"]["TEST"]["ELEMENT"]
is a table, so you need to loop around it and find the correct attribute using element._attr["attr"] == "A2"
.
This is the version that is rewritten for the DOM Handler
:
\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
local domobject = require "luaxml-domobject"
sample = [[
<DATA>
<TITLE>Quantum vacuum ç under mixed boundary conditions: the case for curved spacetime</TITLE>
<NAME="name" />
<VERSION="1" />
<VARIABLES>
<TEST>
<ELEMENT attr="A1">123.000000001</ELEMENT>
<ELEMENT attr="A2">789.000000001</ELEMENT>
</TEST>
</VARIABLES>
</DATA>]]
local dom = domobject.parse(sample)
tex.sprint(dom:query_selector("TITLE")[1]:get_text())
for _, element in ipairs(dom:query_selector("VARIABLES TEST ELEMENT")) do
if element:get_attribute("attr") == "A2" then
tex.sprint(element:get_text())
end
end
\end{luacode*}
\end{document}
You can parse the XML file using domobject.parse()
function. It returns object that contain various methods. The most useful is dom:query_selector
which supports CSS like selectors. For example
dom:query_selector("TITLE")
will return all <TITLE>
elements in the document. You can select the first one using usual Lua table index. This selected object still has support for DOM methods, so you can retrieve text using the get_text
method:
dom:query_selector("TITLE")[1]:get_text()
The second query loops over all VARIABLES/TEST/ELEMENT
. It retrieves the attr
attribute from each of them using the get_attribute
method, and prints contents of the element where the attribute matches value A2
.
This is the result:
Regarding this question:
It is possible both direct UTF and ç
to work my XML?
I guess yes? What issue do you have?