From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: How could I find the 'depth' of an element found with xml.first()?
Date: Thu, 18 Jun 2020 19:04:40 +0200 [thread overview]
Message-ID: <bccf098d-6bc1-3680-c493-4e906ecf2e7f@xs4all.nl> (raw)
In-Reply-To: <6A3BBDEF-EE08-4749-8FD9-7E35FB928199@rna.nl>
On 6/18/2020 3:24 PM, Gerben Wierda wrote:
> Given the following XML snippet
>
> <opa id=“opa">
> <Foo name="SubFoo Level One" id="Foo">
> <Foo name="SubFoo Level Two" id="Foo_2">
> <bar id="Bar" name="A Bar">
> <bar name="Bar Level Three" id="Bar_2">
> <Foo id="oma">OmaFoo</Foo>
> </bar>
> </bar>
> <Foo name="Fuzz Level Four" id="Fuzz"/>
> </Foo>
> <Foo name="SubFoo Level Two" id="Foo_3">
> <fuzz name="Fuzz" id="Fuzz_2"/>
> </Foo>
> </Foo>
> </opa>
>
>
> How would I go about to find the depth of an item found with an LPATH? E.g.
>
> //[id="Foo_3"] would find a node at depth 2 (assuming <opa> is at level 0) while //[id=“Fuzz_2"] would find a node at depth 3 and in either cases I would like to get that number (2 or 3) as well as the actual node. I can assume the values for ‘id’ are unique.
>
> I was wondering how I could go about finding the ‘depth’ of an item in the XML when using the LMTX built-in functions to work with XML. I don’t think there is a default function for that, is there?
You don't deserve an answer because this is not a MWE!
\startbuffer[test]
<something>
<what>
<what>
here
</what>
</what>
</something>
\stopbuffer
\startxmlsetups xml:mysetups
\xmlsetsetup{#1}{something|what}{xml:*}
\stopxmlsetups
\xmlregistersetup{xml:mysetups}
\startxmlsetups xml:something
\xmlflush{#1}
\stopxmlsetups
\startluacode
function xml.finalizers.tex.depth(t)
local d = 0
local t = t and t[1]
while t do
d = d + 1
t = xml.parent(t)
if not t then
break
end
end
context(d)
end
\stopluacode
\startxmlsetups xml:what
\xmldoifelse {#1} {..[name() == "what"]} {
YES
} {
NOP
}
[\xmlflush{#1}]
(\xmlfilter{#1}{./depth()})
\stopxmlsetups
\starttext
\xmlprocessbuffer{main}{test}{}
\stoptext
I might add it anyway (a faster variant that is),
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________
prev parent reply other threads:[~2020-06-18 17:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-18 13:24 Gerben Wierda
2020-06-18 17:04 ` Hans Hagen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bccf098d-6bc1-3680-c493-4e906ecf2e7f@xs4all.nl \
--to=j.hagen@xs4all.nl \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).