ConTeXt XML: Nested section elements

I am trying to produce PDF from XML with ConTeXt. Currently, I am struggling with this: The XML file is in JATS, and JATS usually does not specify the levels of sections. There is an optional attribute disp-level that I have used until now, but it turns out that this attribute is often ommitted. This means I have to guess at which section level we are at. In order to do I have tried checking how far we are away from <body>: If <body> is the parent of the current element the current element's level is section, if it is the parent of the parent of the current element the current element's level is subsection, and so on.

\startbuffer[test]
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.0 20120330//EN"
"JATS-journalpublishing1.dtd">
<front>
</front>
<body>
<p>Some Text</p>
<p>More Text</p>
</sec>
</sec>
</body>
<back>
</back>
</article>
\stopbuffer

\startxmlsetups xml:jatssetups
\xmlsetsetup{#1}{*}{-}
\xmlsetsetup{#1}{article|body|sec|p}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:jatssetups}

\startxmlsetups xml:article
\xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:body
\startdocument
\xmlflush{#1}
\stopdocument
\stopxmlsetups

\startxmlsetups xml:p
\xmlflush{#1}\par
\stopxmlsetups

% Sections

\startxmlsetups xml:sec
\xmlfilter{#1}{../../body/command(xml:sec:section)}
\stopxmlsetups

\startxmlsetups xml:sec:section
\startsection[title=\xmlfirst{#1}{/title}]
\xmlall{#1}{/!title}
\stopsection
\stopxmlsetups

\startxmlsetups xml:sec:subsection
\startsubsection[title=\xmlfirst{#1}{/title}]
\xmlall{#1}{/!title}
\stopsubsection
\stopxmlsetups

\xmlprocessbuffer{main}{test}{}


However, this produces an fatal error ("TeX capacity exceeded."). Perhaps someone has a better idea?

One option is to map the <section> tags to \startsectionlevel macros, which can be nested:

\starttext
Some text
More text
\stopstructurelevel
\stopstructurelevel
\stoptext


The default mapping is that the first level maps to a chapter, the second level maps to a section, and so on. You can change this setting using:

\definesectionlevels
[default]
[
section,
subsection,
subsubsection,
subsubsubsection,
]


With the above setup, the first level \startstructurelevel is mapped to \startsection, the second level \startstructurelevel is mapped to \startsubsection and so on until four nested levels.

You can change the style of the document using normal setup commands that change the style of \startsection etc. Here is an example:

\definesectionlevels
[default]
[
section,
subsection,
subsubsection,
subsubsubsection,
]

\starttext
Some text
More text
\stopstructurelevel
\stopstructurelevel
\stoptext


which gives

Based on Aditya's input I can now come up with this solution:

\startbuffer[test]
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.0 20120330//EN"
"JATS-journalpublishing1.dtd">
<front>
</front>
<body>
<p>Some Text</p>
<p>More Text</p>
</sec>
</sec>
</body>
<back>
</back>
</article>
\stopbuffer

\startxmlsetups xml:jatssetups
\xmlsetsetup{#1}{*}{-}
\xmlsetsetup{#1}{article|body|sec|p}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:jatssetups}

\startxmlsetups xml:article
\xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:body
\startdocument
\xmlflush{#1}
\stopdocument
\stopxmlsetups

\startxmlsetups xml:p
\xmlflush{#1}\par
\stopxmlsetups

% Sections

\startxmlsetups xml:sec
\startstructurelevel [title=\xmlfilter{#1}{/title/command(xml:sec:title)}]
\xmlflush{#1}
\stopstructurelevel
\stopxmlsetups

\startxmlsetups xml:sec:title
\xmlflush{#1}
\stopxmlsetups

\definestructurelevels [default] [section,subsection,subsubsection]

\xmlprocessbuffer{main}{test}{}