Create Separate File using LuaLaTeX

lualuacodeluatex

My Input LaTeX FileName is Article05.tex. I would like to find the text <stepn number>(.-)</stepn number> and store the text is individual file like name is Article05-step.tex. How to achieve this in LuaLaTeX?

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 = [[
<?xml version="1.0" encoding="utf-8"?>
<art>
<title>Scattering of flexural waves an electric current</title>
<para>Smart testing structures are components <step1>Reduce acoustic noise.</step1> used in engineering applications that are capable of sensing or reacting to their environment <step3>Change their mechanical properties.</step3> in a predictable and desired manner. In addition to <step2>Carrying mechanical loads</step2>, <!--may smart structures--> alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para>
</art>]]
treehandler = handler.simpleTreeHandler()
x = xml.xmlParser(treehandler)
x:parse(sample)
tex.sprint(treehandler.root["art"]["title"])

for _, element in ipairs(treehandler.root["art"]["para"]) do
  --if element._attr["attr"] == "A2" then
  --tex.print("\\\\")
  --tex.sprint(element)
  --end
end
\end{luacode*}
\end{document}

In the Article05-step.tex should be:

STEP1: Reduce acoustic noise.
STEP2: Change their mechanical properties.

Best Answer

Try this:

\documentclass{article}
\usepackage{luacode}


\begin{document}

\begin{luacode*}
local domobject = require "luaxml-domobject"
sample = [[
<?xml version="1.0" encoding="utf-8"?>
<art>
<title>Scattering of flexural waves an electric current</title>
<para>Smart testing structures are components <step1>Reduce acoustic noise.</step1> used in engineering applications that are capable of sensing or reacting to their environment <step2>Change their mechanical properties.</step2> in a predictable and desired manner. In addition to carrying mechanical loads, <!--may smart structures--> alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para>
</art>
]]

local f = io.open(tex.jobname .. "-step.tex", "w")
local dom = domobject.parse(sample)
local steps = {}
dom:traverse_elements(function(el)
-- detect if element is named stepN
local number = el:get_element_name():match("^step([0-9]+)")
if number then
  steps[#steps+1] = {number = tonumber(number), text = el:get_text()}
  -- f:write("STEP" .. number ..": " ..  .. "\n")
end
end)
table.sort(steps, function(a, b) return a.number < b.number end)
for k,v in ipairs(steps) do
  f:write("STEP" .. v.number ..": " .. v.text .. "\n")
end
  
f:close()
\end{luacode*}
\end{document}

It uses LuaXML's DOM object. The dom:traverse_elements function loops over all elements, detects if the element name starts with step and save it's text content to the \jobname-step.tex file.

This is the result:

STEP1: Reduce acoustic noise.
STEP2: Change their mechanical properties.
Related Question