5

I have a very (very) long XML file, and I am using ConTeXt to turn it into a book.

This works to read the file and output as-is:

\starttext

\startbuffer[test]
\input /Users/ramfub/Documents/complete_advent.xml
\stopbuffer

\getbuffer[test]

\stoptext

If I copy the contents of the XML file between the \startbuffer and \stopbuffer commands, it works fine:

\startbuffer[test]
<EveningPrayer>
<TitleSection>
<Title>First Sunday of Advent</Title>
<PsalterWeek>Psalter, Week I</PsalterWeek>
<Hour>Evening Prayer I</Hour>
</TitleSection>
<Hymn>
...
<CollectLine></CollectLine>
<CollectLine></CollectLine>
</Collect>
</DaytimePrayer>

\stopbuffer

(xmlsetups)

\xmlprocessbuffer{test}{test}{}

However, if I try to read the XML file into the buffer, it does not work (no errors, but no output at all):

\startbuffer[test]
\input /Users/ramfub/Documents/complete_advent.xml
\stopbuffer

(xmlsetups)

\xmlprocessbuffer{test}{test}{}

I don't want to have to copy 10,000+ pages of XML into my .tex file.

  • There is \xmlprocessfile. – Henri Menke Jan 12 at 19:27
4

You don't have to read the file into a buffer first. There is \xmlprocessfile to directly process an XML file.

enter image description here

For more info read the XML manual (PDF).


If you want to read the file into a buffer for some reason, e.g. to preprocess it in Lua or so, you can easily implement such a macro yourself.

\startluacode
function buffers.fromfile(name,file)
    if not buffers.exists(name) then
        buffers.assign(name,io.loaddata(resolvers.findfile(file)))
    end
end

interfaces.implement {
    name      = "readbufferfromfile",
    actions   = buffers.fromfile,
    arguments = { "string", "string" }
}
\stopluacode

\unprotect

\unexpanded\def\readbufferfromfile{\dodoubleempty\doreadbufferfromfile}
\def\doreadbufferfromfile[#1][#2]{\clf_readbufferfromfile{#1}{#2}}

\protect

\starttext

\readbufferfromfile[name][test.xml]

\typebuffer[name]

\stoptext

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.