% XML processing instructions

% Turn a phone number into a tel:link - without spaces
\def\phonelink#1%
  {\goto{#1} [url(tel:\cldcontext{string.nospaces("#1")})]}

% Derive an individual's name
\def\name#1%
  {\ifxmlattempty{#1}{first_name} {\xmlatt{#1}{initials}} \else {\xmlatt{#1}{initials}, \xmlatt{#1}{first_name}} \fi
   \ifxmlattempty{#1}{maiden_name} {} \else { \tfxx(\xmlatt{#1}{maiden_name})} \fi}

% Create a mailto:link that associates a individual's name with their email address.
\def\nameemaillink#1%
  {\ifxmlattempty{#1}{email} {\name{#1}} \else {\goto{\name{#1}} [url(mailto:\xmlatt{#1}{email})]} \fi}

% Make an individual's mobile phone number a tel:link.
\def\mobilelink#1%
  {\ifxmlattempty{#1}{mobile} {} \else {\phonelink{\xmlatt{#1}{mobile}}} \fi}

% Track these several elements.
\startxmlsetups xml:list:base
  \xmlsetsetup{#1}{addressBook|family|address|members|member}{xml:list:*}
\stopxmlsetups

\xmlregisterdocumentsetup{list}{xml:list:base}

% When we encounter the root <addressBook> element merely flush the rest.
\startxmlsetups xml:list:addressBook
  \xmlflush{#1}
\stopxmlsetups

% Place each family in a frametext box
\startxmlsetups xml:list:family
  \startframedtext[width=\textwidth]
    {\tfb \bf \xmlatt{#1}{surname}\ifxmlattempty{#1}{prefix}{}\else{, \xmlatt{#1}{prefix}}\fi}\blank
    \xmlflush{#1}
  \stopframedtext
  \blank[big]
\stopxmlsetups

% Enumerate the address information
\startxmlsetups xml:list:address
  \xmlatt{#1}{street} \xmlatt{#1}{housenumber}\crlf
  \xmlatt{#1}{postcode} \xmlatt{#1}{city}\crlf
  \ifxmlattempty{#1}{telephone}{\vskip -1em}\else{\phonelink{\xmlatt{#1}{telephone}}}\fi
  %\vskip -1em
  \xmlflush{#1}
  \blank[big]
\stopxmlsetups

% The only purpose of the <members> element is to make a table of members.
\startxmlsetups xml:list:members
  \starttabulate[|l|l|l|]
    \xmlflush{#1}
  \stoptabulate
\stopxmlsetups

% Each <member> gets his own table row.
\startxmlsetups xml:list:member
  \NC \nameemaillink{#1} \NC \xmlatt{#1}{birthday} \NC \mobilelink{#1} \NC\NR
\stopxmlsetups

% finis
