one text item per line output
Monday, 26 December 2011 · 5 messages · last reply 27 Dec 2011
J
jidanni26 Dec 2011 06:35 #
Do I really need to do all this just to get one item per line $ basex -s format=no -q 'doc("...")//.../*:name/concat(text(),"
")' AM 594 復興1
AM 612 中央
AM 630 台灣
AM 657 正聲
I use -s format=no to turn off spaces.
-s format=text not much help.
There really ought to be a -s option to say I want newlines instead of spaces.
In perl all we need is perl -l.
OK, maybe Windows users also need CRLFs (\r\n).
CG
Christian Grün26 Dec 2011 23:44 #
Quoted text · jidanni · 2 lines
> Do I really need to do all this just to get one item per line > $ basex -s format=no -q 'doc("...")//.../*:name/concat(text(),"
")'
By default, there won't be any delimiting character between text nodes, which is why it wouldn't help to replace spaces with newlines. However, you may want to use the -w flag to preserve the original document whitespaces and newlines.
Christian
J
jidanni27 Dec 2011 01:38 #
Quoted text · Christian Grün · 1 line
>>>>> "CG" == Christian Grün <christian.gruen at gmail.com> writes:
CG> However, you may want to use the -w flag to preserve the original CG> document whitespaces and newlines.
I would have never guessed from the man page, which just mentions input, not output:
-w By default, whitespaces around text nodes are chopped when a database is created. The flag specifies to suppress chopping.
Nor is -w mentioned on http://docs.basex.org/wiki/Serialization
Yes it turns out -w is what I want.
$ basex -q 'doc("u.xml")'
<W>
<X>AA AA</X>
<X>BB BB</X>
<X>CC CC</X>
<X>DD DD</X>
</W>$ basex -q 'doc("u.xml")'; echo <-------note the echo needed to add the missing newline [BUG].
<W>
<X>AA AA</X>
<X>BB BB</X>
<X>CC CC</X>
<X>DD DD</X>
</W>
$ basex -w -s method=text -q 'doc("u.xml")' AA AA
BB BB
CC CC
DD DD
$ basex -w -s method=text -q 'doc("u.xml")'|sed 's/^ \+//;/^$/d' #OK, this is what I want. Thanks. AA AA
BB BB
CC CC
DD DD
J
Wait, -w falls apart when just wanting the X nodes,
$ basex -w -s method=text -q 'doc("u.xml")'
AA AA
BB BB
CC CC
DD DD
$ basex -w -s method=text -q 'doc("u.xml")//X'; echo
AA AABB BBCC CCDD DD
$
J
Last year's discussion https://mailman.uni-konstanz.de/pipermail/basex-talk/2010-October/000758.html Related discussion http://www.stylusstudio.com/xquerytalk/201012/003341.html
Anyways, proof that basex is "unfair":
$ basex -q 'doc("u.xml")/W'; echo
<W>
<X>AA AA</X>
<X>BB BB</X>
<X>CC CC</X>
<X>DD DD</X>
</W>
$ basex -w -s method=text -q 'doc("u.xml")/W'; echo AA AA
BB BB
CC CC
DD DD
$ basex -q 'doc("u.xml")/W/X'; echo
<X>AA AA</X>
<X>BB BB</X>
<X>CC CC</X>
<X>DD DD</X>
$ basex -w -s method=text -q 'doc("u.xml")/W/X'; echo
AA AABB BBCC CCDD DDUnfair!
Written for BaseX 7.0. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery