On 25.02.2017 21:49, Hans Hagen wrote:
xml.attribute(e,"..[tag()='section' or tag()='chapter']","title")
with an optional default
(there is also p = xml.parent(e) and then you can access p.at.title ..
so there are several ways(
Hans
I am sorry it took me so long to play with this... But it still doesn't
do what I need. My problem is still: from a given element, I need to
grab the content of an attribute that may be two or three or four levels
up in the xml tree. The code below (adapted from Hans) still only works
for a parent, but I need access to an "ancestor."
Thomas
\starttext
\startbuffer[test]
<document>
<chapter title="OneC">
<section title="OneS">
<subsection title="OneSS">
Subsection Text
</subsection>
</section>
</chapter>
<chapter title="TwoC">
<subsection title="OneSS">
Subsection Text
</subsection>
</chapter>
</document>
\stopbuffer
\startluacode
function xml.functions.xml_subsection(e)
chaptertitle = xml.attribute(e, "..[tag()='chapter']", "title")
context("Title of chapter is: ")
context(chaptertitle)
context.par()
lxml.flush(e)
context.blank( { "2*line" })
end
\stopluacode
\startxmlsetups xml:test:setups
\xmlsetsetup{\xmldocument}{*}{xml:*}
\stopxmlsetups
\xmlregistersetup{xml:test:setups}
\startxmlsetups xml:document
\xmlflush{#1}
\stopxmlsetups
\startxmlsetups xml:chapter
\xmlflush{#1}
\stopxmlsetups
\startxmlsetups xml:section
\xmlflush{#1}
\stopxmlsetups
\startxmlsetups xml:subsection
\xmlfunction{#1}{xml_subsection}
\stopxmlsetups
\xmlprocessbuffer{main}{test}{}
\stoptext
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the
Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________