有没有办法在 Saxon-HE 9.6 的多个匹配节点上获取换行符分隔的输出?
我使用这个名为 saxon-lint
的包装器:
#!/bin/bash
java -cp /path/to/saxon9he.jar net.sf.saxon.Query -s:"$2" -qs:"$1" !method=text
示例:
文件.xml
<house_pet_hazards>
<hazard type="cleanup">
<name>hairballs</name>
<guilty_party species="cat">Dilly</guilty_party>
<guilty_party species="cat">Nameless</guilty_party>
<guilty_party species="cat">Katie</guilty_party>
</hazard>
</house_pet_hazards>
命令行:
$ saxon-lint '/house_pet_hazards/hazard/guilty_party' file.xml
输出:
DillyNamelessKatie
预期输出:
Dilly
Nameless
Katie
编辑
I've found a semi-working solution by searching google hardly 提出的 Michael Kay ,所以我写了这个包装器:
#!/bin/bash
xpath="$1"
[[ -s $2 ]] && file="$(readlink -f "$2")" || file=/dev/stdin
java -cp saxon9he.jar net.sf.saxon.Query -qs:"for \$x in doc('file://$file')$xpath
return (data(\$x),text{' '})" !method=text
输出:
$ saxon-lint "/house_pet_hazards/hazard/guilty_party" /tmp/file.xml
Dilly
Nameless
Katie
但是我发现的问题是,当我使用路径的时候
house_pet_hazards/hazard/guilty_party
我得到错误
Error on line 1 column 37
XPST0003 XQuery syntax error near #...p/file.xml')house_pet_hazards/#:
expected "return", found name "house_pet_hazards"
Static error(s) in query
或
./house_pet_hazards/hazard/guilty_party
我收到错误:
Error on line 1 column 37
XPST0003 XQuery syntax error near #... doc('file:///tmp/file.xml')./#:
expected "return", found "."
Static error(s) in query
The wrapper
最佳答案
当然,只需将您的查询字符串更改为:
-qs:"string-join(/house_pet_hazards/hazard/guilty_party,'
')"
您也可以缩短到 //guilty_party
的路径。
Cygwin 中的示例 bash 脚本:
[/cygdrive/c/apps/SaxonHE9-6-0-3J]
==> cat saxon-lint.bsh
#!/bin/bash.exe
java -cp saxon9he.jar net.sf.saxon.Query -s:$2 -qs:"string-join($1,'
')" !method=text
[/cygdrive/c/apps/SaxonHE9-6-0-3J]
==> ./saxon-lint.bsh //guilty_party so.xml
Dilly
Nameless
Katie
关于xml - 来自命令行的 Saxon-HE 9.6 的换行符分隔输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27475096/