是否有一种简单的方法,可能使用Linux中的开源命令行工具,从给定的XML文档中剥离超出给定阈值的所有级别,而不管结构如何?
输入:
<a att="1">
<b/>
<c bat="2">
<d/>
</c>
</a>
输出,等级= 1:
<a att="1"/>
输出,等级= 2:
<a att="1">
<b/>
<c bat="2"/>
</a>
我尝试过XPath但无法限制级别。
答案 0 :(得分:3)
在XSLT中非常简单:
<xsl:template match="*">
<xsl:if test="count(ancestor::*) <= $level">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>
答案 1 :(得分:3)
在XQuery中,它与XSLT中的几乎相同:
copy $output := $input
modify delete nodes $output//node()[count(ancestor::*) eq $level]
return $output
答案 2 :(得分:2)
或者,如果没有XQuery Update,请再次解构并将树组合在一起,直到达到最高级别:
declare function local:limit-level($element as element(), $level as xs:integer) {
if ($level gt 0)
then
element {node-name($element)} {
$element/@*,
(
for $child in $element/node()
return local:limit-level($child, $level - 1)
)
}
else ()
};
local:limit-level(/*, 2)