如何排除父元素和子元素节点
how exclude parent element and sub element nodes
如何排除 "Approach"" 和 "Amount1" 元素。我可以排除 "Amount1" 但无法同时消除两个节点 (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))]
结果return:
<root>
<Approach> Approach </Approach>
<Progress> Progress </Progress>
<Objective> Objective </Objective>
<fundingSources>
<Source>
<Amounts>
<Amount2>millions</Amount2>
</Amounts>
</Source>
</fundingSources>
</root>
但努力结合另一个父节点名"Approach"省略。谢谢
您需要一种递归方法。 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!
如何排除 "Approach"" 和 "Amount1" 元素。我可以排除 "Amount1" 但无法同时消除两个节点 (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))]
结果return:
<root>
<Approach> Approach </Approach>
<Progress> Progress </Progress>
<Objective> Objective </Objective>
<fundingSources>
<Source>
<Amounts>
<Amount2>millions</Amount2>
</Amounts>
</Source>
</fundingSources>
</root>
但努力结合另一个父节点名"Approach"省略。谢谢
您需要一种递归方法。 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!