Peter-Paul
Peter-Paul

Reputation: 85

Using XQuery to create valid Latex file based on XML data

I know this task is somewhat unorthodox, but I hope someone is able to help me. I'm trying to create a valid Latex file using XQuery, based on XML file on products. The XML file (xml/products.xml) looks like this:

<?xml version="1.0"?>
<Products>
    <Maker name = "A">
        <PC model = "1001" price = "2114">
            <Speed> 2.66 </Speed>
            <RAM> 1024 </RAM>
            <HardDisk> 250 </HardDisk>
        </PC>

This is the latex output I need:

\documentclass[]{article}

\begin{document}

\begin{center}
\begin{tabular}{| l | l |}
\hline
Price & Model \\ \hline
price value & model value \\ \hline

\end{tabular}
\end{center}

\end{document}

And I've created the following (character escaping) XQuery to produce the required Latex:

let $oc := "&#123;" (: for { :)
let $cc := "&#125;" (: for } :)
let $space := "&#32;" (: space :)
let $tab := "&#9;" (: tab :)
let $ampersand := "&#38;" (: ampersand :)

\\documentclass\[\]{$oc}article{$cc}

\\begin{$oc}document{$cc}

\\begin{$oc}center{$cc}
    \\begin{$oc}tabular{$cc}{$oc}| l | l |{$cc}
    \\hline
    Model{$ampersand}Price{$space}\\\\{$space}\\hline
    {for $pc in doc("xml/products.xml")/Products/Maker/PC
let $price:=data($pc/@price)
let $model:=data($pc/@model)
return
{$model}{$ampersand}{$price} \\\\ \\hline
}
\\end{$oc}tabular{$cc}
\\end{$oc}center{$cc}

\\end{$oc}document{$cc}

I can't get the query to work.. :(. And as usual, XQuery does not give much debug info to go on. Are there smarter ways of escaping characters using XQuery? Or perhaps just printing text directly?

Upvotes: 1

Views: 514

Answers (2)

Peter-Paul
Peter-Paul

Reputation: 85

I restructured the concat from Christian's approach a bit, and now it works. Below the code:

    declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
    declare option output:method "text";

    let $stringa:=string("
    \documentclass[]{article}
    \begin{document}
    \begin{center}
    \begin{tabular}{| l | l |} \hline
    Model &amp; Price \\ \hline
    ")

    return ($stringa),
        (
        for $pc in doc("xml/products.xml")/Products/Maker/PC
        let $price := data($pc/@price)
        let $model := data($pc/@model)

        return concat($model, " &amp; ", $price, " \\ \hline")
        ),
    (
    string("
    \end{tabular} 
    \end{center} 
    \end{document}
    ")
    )

Upvotes: 0

Christian Gr&#252;n
Christian Gr&#252;n

Reputation: 6229

This is one possible solution:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "text";

concat("\documentclass[]{ article }

\documentclass[]{article}

\begin{document}

\begin{center}
\begin{tabular}{| l | l |}
\hline
Price &amp; Model \\ \hline
",
for $pc in doc("xml/products.xml")/Products/Maker/PC
let $price := data($pc/@price)
let $model := data($pc/@model)
return concat($model, " &amp; ", $price, " \\ \hline
"),

"\end{tabular}
\end{center}

\end{document}")

Note that you may need to further modify the data returned from products.xml in order to make it conformant with the LateX syntax.

Upvotes: 2

Related Questions