Reputation: 85
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 := "{" (: for { :)
let $cc := "}" (: for } :)
let $space := " " (: space :)
let $tab := "	" (: tab :)
let $ampersand := "&" (: 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
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 & 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, " & ", $price, " \\ \hline")
),
(
string("
\end{tabular}
\end{center}
\end{document}
")
)
Upvotes: 0
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 & 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, " & ", $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