Lua 多重 for 循环语句

Lua 多重 for 循环语句

我的XML样子是:

<contrib-group content-type="all">
    <contrib contrib-type="author"><name><surname>Bronnikov</surname><given-names>Kirill A</given-names></name>
    <xref ref-type="aff" rid="f1">1</xref>
    <xref ref-type="aff" rid="f2">2</xref>
    <xref ref-type="aff" rid="f3">3</xref>
    <xref ref-type="aff" rid="cqgab7bbaem1"/>
    <ext-link ext-link-type="orcid">0000-0001-9392-7558</ext-link>
    </contrib>
    <contrib contrib-type="author"><name><surname>Santos</surname><given-names>N O</given-names></name>
    <xref ref-type="aff" rid="f4">4</xref>
    <xref ref-type="aff" rid="f5">5</xref>
    <xref ref-type="aff" rid="cqgab7bbaem2"/>
    <ext-link ext-link-type="orcid">0000-0003-4038-5729</ext-link>
    </contrib>
    <contrib contrib-type="author"><name><surname>Wang</surname><given-names>Anzhong</given-names></name>
    <xref ref-type="aff" rid="f6">6</xref>
    <xref ref-type="aff" rid="f7">7</xref>
    <xref ref-type="fn" rid="fn1">8</xref>
    <xref ref-type="aff" rid="cqgab7bbaem3"/>
    <ext-link ext-link-type="orcid">0000-0002-8852-9966</ext-link>
    </contrib>
    </contrib-group>

我的Lua是:

local author_text = function(e)
    local authors = {}
    for _, author in ipairs(e:query_selector("contrib name")) do
         local snm = ""
         local fnm = ""
         for _, nm in ipairs(e:get_children(author)) do
            if nm:get_element_name() == "surname" then
            snm = nm:get_text()
            elseif nm:get_element_name() == "given-names" then
            fnm = nm:get_text()
        end
    end
         table.insert(authors, "\\fmauthor{"..fnm.." "..snm.. "}")
    end
      return table.concat(authors, "")
end

我期望的结果是:

\fmauthor{Kirill A Bronnikov}\fmorcid{0000-0001-9392-7558}
\fmauthor{N O Santos}\fmorcid{0000-0003-4038-5729}
\fmauthor{Anzhong Wang}\fmorcid{0000-0002-8852-9966}

我已经获得了 authorsurnamegiven-namesonly。如何获取ext-link文本值?

答案1

只是一个建议:发布一个完整的示例,因为您的示例luaxml没有提及。如果每个贡献只有一位作者,则以下内容将起作用,但应该很容易将其适应其他场景:

local author_text = function(e)
    local authors = {}
    for _, name in ipairs(e:query_selector("contrib")) do
        local oo = (name:query_selector("ext-link")[1]):get_text() or ""
        local fnm = (name:query_selector("name given-names")[1]):get_text() or ""
        local snm = (name:query_selector("name surname")[1]):get_text() or ""
        table.insert(authors, string.format("\\fmauthor{%s %s}\\ \\fmorcid{%s}", fnm, snm, oo))
    end
    return table.concat(authors, "")
end

我对 Lua 的理解比 XML 好得多,因此我很乐意提供任何有用的评论。

相关内容