9

Using the .NET version of Saxon 9.4, I run a command line like:

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

and I get a result like

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

How can I turn off the XML header (omit the XML declaration) so that I just get 375 as output? I've checked the documentation, but perhaps I'm just missing it.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
Andrew
  • 14,325
  • 4
  • 43
  • 64
  • If using `saxon-js` (JS/Node) via `saxon.serialize(saxon.transform(..))` (with just a SEF), you can extract the output parameters from the SEF itself and pass them as the second parameter of `serialize()` (otherwise the serializer will not know/honor the desired output config, `omit-xml-declaration` etc.): `let outConf = sef.C.find(c => c.N === "output") .C.filter(c => c.N === "property").reduce((params, c) => { params[c.name] = c.value === "yes" ? true : c.value === "no" ? false : c.value; return params; }, {}); let result = saxon.serialize(saxon.transform(..), outConf);` – Janaka Bandara Mar 23 '21 at 07:25

2 Answers2

9

Figured it out...

I needed to add the omit-xml-declaration option:

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()"

would be one way to accomplish this.

Andrew
  • 14,325
  • 4
  • 43
  • 64
  • 5
    You can also set serialization parameters directly from the command line, e.g. !method=text (this isnt quite the same as omit-xml-declaration, it also prevents < and & being escaped) – Michael Kay Feb 17 '12 at 08:51
2

Based on this comment from Michael Kay, you can also use a command line parameter.

Below is the command using Java:

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

From the command line help:

Saxon-HE 9.8.0.11J from Saxonica
Usage: see http://www.saxonica.com/documentation/index.html#!using-xsl/commandline
Format: net.sf.saxon.Transform options params
Options available: -? -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
Use -XYZ:? for details of option XYZ
Params:

 param=value           Set stylesheet string parameter
 +param=filename       Set stylesheet document parameter
 ?param=expression     Set stylesheet parameter using XPath
 !param=value          Set serialization parameter
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49