【问题标题】:Turn off xml header output in Saxon关闭 Saxon 中的 xml 标头输出
【发布时间】:2012-03-08 09:26:04
【问题描述】:

使用 Saxon 9.4 的 .NET 版本,我运行如下命令行:

Query.exe -s:myfile.xml -qs:/cruisecontrol/build/msbuild[@success='true']/project[1]/target[@name='GetLatestSource']/message[last()]/text()

我得到类似的结果

<?xml version="1.0" encoding="UTF-8"?>375

我怎样才能关闭 XML 标头(省略 XML 声明),以便得到 375 作为输出?我检查了the documentation,但也许我只是想念它。

【问题讨论】:

  • 如果通过saxon.serialize(saxon.transform(..)) 使用saxon-js (JS/Node)(只有一个SEF),您可以从SEF 本身提取输出参数并将它们作为serialize() 的第二个参数传递(否则序列化程序将不知道/尊重所需的输出配置,omit-xml-declaration 等):let outConf = sef.C.find(c =&gt; c.N === "output") .C.filter(c =&gt; c.N === "property").reduce((params, c) =&gt; { params[c.name] = c.value === "yes" ? true : c.value === "no" ? false : c.value; return params; }, {}); let result = saxon.serialize(saxon.transform(..), outConf);

标签: xml xpath saxon


【解决方案1】:

想通了……

我需要添加omit-xml-declaration 选项:

Query.exe -s:myfile.xml -qs:"declare option saxon:output 'omit-xml-declaration=yes'; /cruisecontrol/build/msbuild[@success='true']/project[1]/target[@name='GetLatestSource']/message[last()]/text()"

将是实现此目的的一种方法。

【讨论】:

  • 您也可以直接从命令行设置序列化参数,例如!method=text(这和 omit-xml-declaration 不太一样,它还可以防止
【解决方案2】:

基于Michael Kay中的this comment,还可以使用命令行参数。

以下是使用 Java 的命令:

java -cp saxon9he.jar net.sf.saxon.Transform -s:mytemplate.xsl -xsl:myfile.xml !method=text

从命令行帮助:

来自 Saxonica 的 Saxon-HE 9.8.0.11J
用法:见http://www.saxonica.com/documentation/index.html#!using-xsl/commandline
格式:net.sf.saxon.Transform 选项参数
可用选项:-? -a -catalog -config -cr -diag -dtd -ea -expand -explain -export -ext -im -init -it -jit -l -lib -license -m -nogo -now -o -opt -or -outval -p -quit -r -relocate -repeat -s -sa -scmin -strip -t -T -target -TB -threads -TJ -Tlevel -Tout -TP -traceout -tree -u -val -versionmsg -warnings -x -xi -xmlversion -xsd -xsdversion -xsiloc -xsl -y
使用 -XYZ:?有关选项 XYZ 的详细信息
参数:

param=value 设置样式表字符串参数 +param=filename 设置样式表文档参数 ?param=expression 使用 XPath 设置样式表参数 !param=value 设置序列化参数

【讨论】:

  • 如果从 bash 执行,您需要转义感叹号:\!method=text
猜你喜欢
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多