# parsing bbl file to an xml file from within pdflatex while typesetting

As editor of a journal I need to translate the bibliography, in a bbl file, into part of an xml file (for potential submission to cross-ref). It seems best done on the fly by pdfLaTeX so I can also record in the xml the main metadata (authors, title, pages, volume, etc). Currently I use a scheme for bbl files where the \bibitem does not involve brackets, e.g. \bibitem{Smith99} ... The problem is that as soon as the bbl file uses brackets (as in natbib) then my scheme fails catastrophically on \bibitem[blah]{Smith99} ... Question: how can I get pdfLaTeX to parse a bbl file, with such \bibitem[]{}, into an xml file while pdflatex is typesetting to pdf?

Currently I do the following (hacked from somewhere in latex and executed \AtBeginDocument):

\renewcommand{\bibitem}[1]{</unstructured_citation></citation>
^^J<citation key="#1"><unstructured_citation>}
...
\def\j@@Input{%
\let\jrnltempb\jrnltempa
\ifeof\jrnlin
\immediate\closein\jrnlin
\else
\immediate\write\jrnlout{\jrnltempb}
\expandafter\j@@Input
\fi}
\typeout{**** Starting to write the bibliography to the xml.}
\immediate\openin\jrnlin\jobname.bbl\relax
\immediate\write\jrnlout{<citation_list>
^^J<citation key="nil"><unstructured_citation>}

Try using xparse, \usepackage{xparse}\DeclareExpandableDocumentCommand{\bibitem}{o{}m}{</unstruct‌​ured_citation></citation>^^J<citation key="#2" opt-arg="#1"><unstructured_citation>}. – Bruno Le Floch May 15 '13 at 11:59
Does this have to be done with the .bbl file? I've seen .bst files that deliberately add an XML section to the .bbl from BibTeX, which is easier as the data contains no formatting. – Joseph Wright Aug 16 '13 at 10:23