【问题标题】:Xslt Saxon processor - All files in a directoryXslt Saxon 处理器 - 目录中的所有文件
【发布时间】:2016-09-02 11:15:38
【问题描述】:

我正在运行这个批处理文件,它处理 /input 目录中的一个 xml 文件。

java  -jar c:/SaxonHE9-6-0-6J/saxon9he.jar -s:D:/hdbook-Convertor/input/  -xsl:C:\Automation\hdbook-convertor.xml  -o:D:\hdbook-Convertor\output -xmlversion:1.0

我希望能够处理子目录和其中的文件,结构为

然后将所有已处理的文件移动到输出文件夹。我看不到如何让-s:D:/hdbook-Convertor/input/ 循环遍历所有子目录?

【问题讨论】:

  • Saxon 应该如何处理各种 pdf 文件?
  • 没什么,它只是获取 xml 并使用样式表重新格式化它

标签: java xml batch-file xslt saxon


【解决方案1】:

尝试在转换本身中使用 collection() 函数:

<xsl:template name="main">
  <xsl:for-each select="collection('file:///D:/hdbook-Convertor/input/?recurse=yes;select=*.xml')">
    <xsl:result-document href="output/{tokenize(document-uri(.), '/')[last()]">
      <xsl:apply-templates/>
    </xsl:result-document>
  </xsl:for-each>
</xsl:template>

【讨论】:

  • 嗨迈克尔,我正在使用这样的样式表 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="/"&gt; &lt;export&gt; &lt;order&gt; &lt;header&gt; &lt;xsl:for-each select="export/order/header"&gt; &lt;id&gt;&lt;xsl:value-of select="id"/&gt;&lt;/id&gt; &lt;datecreated&gt;&lt;xsl:value-of select="datecreated"/&gt;&lt;/datecreated&gt; 我将如何在您的示例中指定源?
  • 只需将提供的模板规则添加到 name="main",然后如果从命令行运行,则使用 -it:main 调用样式表,省略 -s(源)选项。
  • 输入目录的路径有问题吗?C:\Automation&gt;java -jar c:/SaxonHE9-6-0-6J/saxon9he.jar -it:main -xsl:C:\Autom ation\hdbook-convertor.xml -o:D:\hdbook-Convertor\output -xmlversion:1.0 Output is a directory, but input is not
  • 指定一个文件作为输出,而不是一个目录。它用于 xsl:result-document 范围之外的任何输出,并且 xsl:result-document 中的相对 URI 是相对于该位置的。
  • 这是一个绝对标准和基本的错误消息,这意味着您未能在样式表中声明“xsl”命名空间前缀。但请:不要在 cmets 中问补充问题。这是一个新问题,所以如果你真的需要问它,那么它应该是一个新问题,而不是对上一个问题的答案的评论。 (话虽如此,这个问题几乎肯定会被否决,因为最基本的阅读或研究会告诉你哪里出了问题。)
【解决方案2】:

试试这个(未经测试)

for /f "delims=" %%f in ('dir /b /s /ad D:\hdbook-Convertor\input') do echo java -jar c:/SaxonHE9-6-0-6J/saxon9he.jar -s:"%%~dpf" -xsl:C:\Automation\hdbook-convertor.xml -o:D:\hdbook-Convertor\output -xmlversion:1.0

如果命令看起来正确且运行良好(手动尝试其中几个),请删除“echo”以进行实际批量执行。

【讨论】:

  • 感谢 parag,这种工作,脚本查看了每个目录,但 saxon 没有执行 xlst 转换。见下面的命令行输出
  • C:\Automation>for /F "delims=" %f in ('dir /b /s /ad D:\hdbook-Convertor\input') do java -jar c:/SaxonHE9 -6-0-6J/saxon9he.jar -s:"%~dpf" -xsl:C:\Automation\hdbook-convertor.xml -o:D:\hdbook-Convertor\output -xmlversion:1.0 C:\Automation >java -jar c:/SaxonHE9-6-0-6J/saxon9he.jar -s:"D:\hdbook-Convertor\输入\" -xsl:C:\Automation\hdbook-convertor.xml -o:D: \hdbook-Convertor\output - xmlversion:1.0 没有来自 Saxonica 的样式表文件名 Saxon-HE 9.6.0.6J 用法:见saxonica.com/html/documentation/using-xsl/commandline.html
  • 格式:net.sf.saxon.Transform options params 可用选项:-? -a -catalog -config -cr -dtd -expand -explain -ext -im -in it -it -l -m -now -o -opt -or -outval -p -quit -r -repeat -s -sa -剥离 -t -T -t 线程 -TJ -TP -traceout -tree -u -val -versionmsg -warnings -x -xi -xmlversion -xsd -xsdversion -xsiloc -xsl -xsltversion -y 使用 -XYZ:?有关选项 XYZ 的详细信息
  • Params: param=value 设置样式表字符串参数 +param=filename 设置样式表文档参数 ?param=expression 使用XPath设置样式表参数 !param=value 设置序列化参数
  • 每次转换都会启动一个新的 JVM,因此转换大量文件需要很长时间。
猜你喜欢
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多