1

I have need to find <ELEMENT attr="A2"> and print in PDF. How to achieve this. My MWE is:

\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"])

tex.sprint(treehandler.root["DATA"]["VARIABLES"]["TEST"]["ELEMENT"])

\end{luacode*}
\end{document}

It is possible both direct UTF and &#x00E7; to work my XML?

1 Answer 1

2

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:

enter image description here

Regarding this question:

It is possible both direct UTF and &#x00E7; to work my XML?

I guess yes? What issue do you have?

6
  • About entities: When i have tested with simpleTreeHandler its showing the error ! String contains an invalid utf-8 sequence.
    – Balaji
    Jul 13, 2020 at 3:30
  • Using Dom Object, I have stored title value in variable Title . How to print afterwards. Example: Title = (dom:query_selector("TITLE")[1]:get_text()). Need to print in maketitle.
    – Balaji
    Jul 13, 2020 at 11:12
  • @Balaji ah I see. it is another thing fixed in the DOM library.
    – michal.h21
    Jul 13, 2020 at 13:28
  • @Balaji just don't put parentheses around: Title = dom:query_selector("TITLE")[1]:get_text()
    – michal.h21
    Jul 13, 2020 at 13:29
  • How to use any characters using RegEx in Lua?
    – Balaji
    Nov 24, 2020 at 2:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .