检测最后一个 post-分组元组
Detect last post-grouping tuple
在 XQuery 3.0 中,如何检测 last post-分组元组(由 XQuery 3.0 spec) produced by the return clause? Is it possible to use something like position() = last()
? What would be the context for the position
函数定义?
例如,假设我想通过 XQuery 生成 CSV 输出。为了在 CSV 输出中分隔行,我在 return
子句生成的每个元组之后附加了一个换行符:
xquery version "3.0";
declare option saxon:output "omit-xml-declaration=yes";
declare option saxon:output "method=text";
declare variable $data := (
<items>
<item>
<property name="a"/>
<property name="a"/>
<property name="b"/>
</item>
<item>
<property name="a"/>
</item>
<item>
<property name="b"/>
<property name="c"/>
<property name="d"/>
</item>
<item>
<property name="b"/>
<property name="c"/>
</item>
</items>
);
for $item in $data/item,
$name in $item/property/@name
group by $name
return (
text{string-join(($name, string(count($item))), ",")},
text{" "}
)
但是,这会在最后一个元组之后留下一个空行。如果我可以测试元组的位置,我就可以避免在最后一个元组之后附加新行。
XQuery 规范似乎确实缺少类似 group by $variable at $position
的内容,类似于 for
子句中允许的内容。再次阅读 XQuery 3.0 specs,我也找不到任何有用的东西。 position()
和 last()
需要节点上下文,这在循环内不可用。
但关于您的潜在问题:为什么不使用另一个 string-join(...)
将项目与中间的换行符连接起来,就像您对计数所做的那样?
string-join(
for $item in $data/item,
$name in $item/property/@name
group by $name
return (
text{string-join(($name, string(count($item))), ",")}
),
text{" "}
)
在 XQuery 3.0 中,如何检测 last post-分组元组(由 XQuery 3.0 spec) produced by the return clause? Is it possible to use something like position() = last()
? What would be the context for the position
函数定义?
例如,假设我想通过 XQuery 生成 CSV 输出。为了在 CSV 输出中分隔行,我在 return
子句生成的每个元组之后附加了一个换行符:
xquery version "3.0";
declare option saxon:output "omit-xml-declaration=yes";
declare option saxon:output "method=text";
declare variable $data := (
<items>
<item>
<property name="a"/>
<property name="a"/>
<property name="b"/>
</item>
<item>
<property name="a"/>
</item>
<item>
<property name="b"/>
<property name="c"/>
<property name="d"/>
</item>
<item>
<property name="b"/>
<property name="c"/>
</item>
</items>
);
for $item in $data/item,
$name in $item/property/@name
group by $name
return (
text{string-join(($name, string(count($item))), ",")},
text{" "}
)
但是,这会在最后一个元组之后留下一个空行。如果我可以测试元组的位置,我就可以避免在最后一个元组之后附加新行。
XQuery 规范似乎确实缺少类似 group by $variable at $position
的内容,类似于 for
子句中允许的内容。再次阅读 XQuery 3.0 specs,我也找不到任何有用的东西。 position()
和 last()
需要节点上下文,这在循环内不可用。
但关于您的潜在问题:为什么不使用另一个 string-join(...)
将项目与中间的换行符连接起来,就像您对计数所做的那样?
string-join(
for $item in $data/item,
$name in $item/property/@name
group by $name
return (
text{string-join(($name, string(count($item))), ",")}
),
text{" "}
)