2017-06-20 30 views
3

如何使用XQuery移除xml中的處理指令標記?Marklogic:使用移除處理指令標記的Xpath

示例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標籤。

回答

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!

+0

很棒@grtjn ..它工作正常。謝謝! – Antony