[NTG-context] XML to ConTeXt: converting date attribute from ISO date
Axel Kielhorn
tex at axelkielhorn.de
Wed Mar 11 14:14:37 CET 2020
> Am 15.08.2018 um 14:04 schrieb Hans Hagen <j.hagen at xs4all.nl>:
>
> On 8/15/2018 12:27 PM, Mike O'Connor wrote:
>> G’Day,
>> Having a first go at converting TEI XML to ConTeXt.
>> Steep learning curve, but easier (for me) than XSLT, and first results were amazing. Using http://www.pragma-ade.com/general/manuals/xml-mkiv.pdf as a guide.
>> Currently stuck on converting an ISO format date (eg. 1908-01-10) into something more readable (eg. 10 January 1908) in the output. I presume I should create a function (?luacode) that can take any ISO value and output the readable form. The following works but seems to me inefficient.
>> Grateful for any pointers.
>> Mike
>> Here is my MWE:
>> \startbuffer[demo]
>> <TEI>
>> <text>
>> <body>
>> <div type="letter">
>> <opener>
>> <date when-iso="1908-01-10" type="divDate">10.I.08</date>
>> </opener>
>> <closer>
>> <date when-iso="1908-03-19" type="secondDate">19 Mar '08</date>
>> </closer>
>> </div>
>> </body>
>> </text>
>> </TEI>
>> \stopbuffer
>> \startxmlsetups xml:initialize
>> \xmlsetsetup{#1}{date}{xml:date}
>> \stopxmlsetups
>> \xmlregistersetup{xml:initialize}
>> \startxmlsetups xml:date
>> \xmldoifelse {#1}{.[@type='divDate']} {
>> \def\docdate{\ctxlua{
>> local tyear = string.sub('\xmlatt{#1}{when-iso}',1,4)
>> local tmonth = string.sub('\xmlatt{#1}{when-iso}',6,7)
>> local tday = string.sub('\xmlatt{#1}{when-iso}',9,10)
>> context.date{d = tday, m = tmonth, y = tyear}
>> }}
>> \docdate[day,month,year] = Document Date \par
>> } {
>> \def\docdate{\ctxlua{
>> local tyear = string.sub('\xmlatt{#1}{when-iso}',1,4)
>> local tmonth = string.sub('\xmlatt{#1}{when-iso}',6,7)
>> local tday = string.sub('\xmlatt{#1}{when-iso}',9,10)
>> context.date{d = tday, m = tmonth, y = tyear}
>> }}
>> \docdate[day,month,year] = Event Date\par
>> }
>> \stopxmlsetups
>> \starttext
>> \xmlprocessbuffer{main}{demo}{}
>> \stoptext
> Can be wikified ...
>
> \startluacode
> function xml.finalizers.tex.MyDate(e,what,how)
> local t = string.split(e[1].at[what],"-")
> context.date(
> { y = t[1], m = t[2], d = t[3] },
> { how }
> )
> end
> \stopluacode
>
> \startxmlsetups xml:date
>
> \xmldoifelse {#1}{.[@type='divDate']} {
> \xmlfilter{#1}{./MyDate("when-iso","day,month,year")}\
> Document Date \par
> } {
> \xmlfilter{#1}{./MyDate("when-iso","day,month,year")}\
> Event Date\par
>
> }
> \stopxmlsetups
I couldn’t get this to work.
Had to change it to:
\startluacode
function xml.finalizers.tex.MyDate(e,what,how)
local ee = e[1].at[what]
local t = (string.split(ee,"-"))
context.date(
{ y = t[1], m = t[2], d = t[3] },
{ how }
)
end
\stopluacode
mtx-context | ConTeXt Process Management 1.03
mtx-context |
mtx-context | main context file: /Volumes/Macintosh HD/usr/local/texlive/context-109/tex/texmf-context/tex/context/base/mkiv/context.mkiv
mtx-context | current version: 2020.01.30 14:13
mtx-context | main context file: /Volumes/Macintosh HD/usr/local/texlive/context-109/tex/texmf-context/tex/context/base/mkiv/context.mkxl
mtx-context | current version: 2020.01.30 14:13
Greetings Axel
More information about the ntg-context
mailing list