如何使用XQuery删除xml中的处理指令标记?
示例XML:
<a>
<text><?test id="1" loc="start"?><b type="bold">1. </b>
Security or protection <?test id="1" loc=="end"?><?test id="1" loc="start"?><b type="bold">2.
</b> Analyse.
<?test id="1" loc="end"?></text>
</a>
预期产出:
<a>
<text><b type="bold">1. </b> Security or protection <b type="bold">2.
</b> Analyse.</text>
</a>
请帮助删除PI tag。
答案 0 :(得分:4)
这样的事情应该有效:
xquery version "1.0-ml";
declare function local:suppress-pi($nodes) {
for $node in $nodes
return
typeswitch ($node)
case element() return
element { fn:node-name($node) } {
$node/@*,
local:suppress-pi($node/node())
}
case processing-instruction() return ()
default return $node
};
local:suppress-pi(<a>
<text><?test id="1" loc="start"?><b type="bold">1. </b>
Security or protection <?test id="1" loc=="end"?><?test id="1" loc="start"?><b type="bold">2.
</b> Analyse.
<?test id="1" loc="end"?></text>
</a>)
HTH!