如何排除 "Approach "和 "Amount1 "元素。我可以排除 "金额1",但无法让这两个节点(Approach,Amount1)都消除。
Sample.xml。
<root>
<Approach> Approach </Approach>
<Progress> Progress </Progress>
<Objective> Objective </Objective>
<fundingSources>
<Source>
<Amounts>
<Amount1>thousands</Amount1>
<Amount2>millions</Amount2>
</Amounts>
</Source>
</fundingSources>
</root>
我可以通过以下方法删除一个 Amount1 节点 fn:doc("sample.xml")/*[not(((descendant-or-self::Amount1))]。
结果返回。
<root>
<Approach> Approach </Approach>
<Progress> Progress </Progress>
<Objective> Objective </Objective>
<fundingSources>
<Source>
<Amounts>
<Amount2>millions</Amount2>
</Amounts>
</Source>
</fundingSources>
</root>
但难以结合另一个父节点名称 "方法 "省略。 谢谢。
你需要一个递归的方法。XSLT可以很好的实现这个功能,但是你也可以用XQuery这样的方法来实现。
xquery version "1.0-ml";
declare function local:filter(
$nodes as node()*
)
as node()*
{
for $node in $nodes
return typeswitch ($node)
case element(Approach) return ()
case element(Amount1) return ()
case element()
return element { node-name($node) } {
$node/@*,
local:filter($node/node())
}
case document-node()
return document {
local:filter($node/node())
}
default return $node
};
let $xml := <root>
<Approach> Approach </Approach>
<Progress> Progress </Progress>
<Objective> Objective </Objective>
<fundingSources>
<Source>
<Amounts>
<Amount1>thousands</Amount1>
<Amount2>millions</Amount2>
</Amounts>
</Source>
</fundingSources>
</root>
return
local:filter($xml)
HTH!