3

I have the following XML data:

\startbuffer[demo]
<?xml version="1.0" encoding="UTF-8"?>
<Book>
<Reading>
<Header>Foo</Header>
<Source>From Source #1</Source>
<Line>Line #1</Line>
<Line>Line #2</Line>
<Line>Line #3</Line>
</Reading>
<Reading>
<Header>Bar</Header>
<Line>Line #4</Line>
<Line>Line #5</Line>
<Line>Line #6</Line>
</Reading>
</Book>
\stopbuffer

\startxmlsetups xml:initialize
    \xmlsetsetup{#1}{Book|Reading|Header|Source|Line}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:initialize}

I want to put the <Header> and <Source> in two columns, but as you see with the second reading, sometimes there is no source. I defined a \ReadingHeader and was going to use:

\startxmlsetups xml:Reading
    \startReadingHeader
    \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:Source
    \xmlflush{#1}
    \stopReadingHeader
\stopxmlsetups

...but this doesn't work if there is no <Source>. I'm sure I'm doing this all wrong. What is the right way?

  • Easy solution (for processing): If the source belongs to the header make it a child element of Header. If you can't change the format you may simply open the reading header when encountering Reading and close it on the first occurrence of Line. – TeXnician Jan 16 at 12:57
5

Depends a bit on what is the desired output when <source> is missing. You can use \xmlfilter{#1}{...} or \xmldoif... etc. Here is one example:

\startbuffer[demo]
<?xml version="1.0" encoding="UTF-8"?>
<Book>
<Reading>
<Header>Foo</Header>
<Source>From Source #1</Source>
<Line>Line #1</Line>
<Line>Line #2</Line>
<Line>Line #3</Line>
</Reading>
<Reading>
<Header>Bar</Header>
<Line>Line #4</Line>
<Line>Line #5</Line>
<Line>Line #6</Line>
</Reading>
</Book>
\stopbuffer

\startxmlsetups xml:initialize
  \xmlsetsetup{#1}{*}{-}
  \xmlsetsetup{#1}{Book|Reading}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:initialize}

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

\definemixedcolumns[ReadingHeader][balance=yes]

\startxmlsetups xml:Reading
  \startReadingHeader
    \xmlfilter{#1}{/Header/command(xml:Header)}
    \column
    \xmlfilter{#1}{/Source/command(xml:Source)}
  \stopReadingHeader
    \xmlfilter{#1}{/Line/command(xml:Line)}
\stopxmlsetups

\startxmlsetups xml:Source
  [\xmlflush{#1}]
\stopxmlsetups

\startxmlsetups xml:Header
    \bold{\xmlflush{#1}}\space
\stopxmlsetups

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

\xmlprocessbuffer{main}{demo}{}

which gives

enter image description here

  • This is brilliant, Aditya! I NEVER would have figured this out! – Neil Mussett Jan 17 at 11:41

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.