我知道这项任务有点不正统,但我希望有人能够帮助我。我正在尝试使用XQuery创建一个有效的Latex文件,基于产品上的XML文件。 XML文件(xml / products.xml)如下所示:
<?xml version="1.0"?>
<Products>
<Maker name = "A">
<PC model = "1001" price = "2114">
<Speed> 2.66 </Speed>
<RAM> 1024 </RAM>
<HardDisk> 250 </HardDisk>
</PC>
这是我需要的乳胶输出:
\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}
我创建了以下(字符转义)XQuery来生成所需的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}
我无法让查询工作.. :(。和往常一样,XQuery没有提供太多的调试信息。是否有更聪明的方法可以使用XQuery转义字符?或者只是直接打印文本?< / p>
答案 0 :(得分:2)
这是一种可能的解决方案:
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}")
请注意,您可能需要进一步修改从products.xml
返回的数据,以使其符合LateX语法。
答案 1 :(得分:0)
我从Christian的方法中重新构建了concat,现在它有效了。代码下方:
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}
")
)