5

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">
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="1.0" article-type="other">
<front>
</front>
<body>
<sec id="section-heading">
  <title>Section Heading</title>
  <p>Some Text</p>
  <sec id="subsection-heading">
    <title>Subsection Heading</title>
    <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?

6
+50

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

\starttext
\startstructurelevel[title=Section heading]
  Some text
  \startstructurelevel[title=Subsection heading]
    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,
  ] 

\setuphead[section][color=blue]
\setuphead[subsection][color=red]

\starttext
\startstructurelevel[title=Section heading]
  Some text
  \startstructurelevel[title=Subsection heading]
    More text
  \stopstructurelevel
\stopstructurelevel
\stoptext

which gives

enter image description here

2

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">
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="1.0" article-type="other">
<front>
</front>
<body>
<sec id="section-heading">
  <title>Section Heading</title>
  <p>Some Text</p>
  <sec id="subsection-heading">
    <title>Subsection Heading</title>
    <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]
\setuphead [section] [style=\bf]
\setuphead [subsection] [style=\em]


\xmlprocessbuffer{main}{test}{}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.