Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marklogic: Xpath using removing processing instruction tag

How to remove the processing instruction tag in xml using XQuery ?

Sample 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>

Expected output :

 <a>
     <text><b type="bold">1. </b> Security or protection <b type="bold">2.
     </b> Analyse.</text>

  </a>

Kindly help to removing PI tags.

like image 432
Antony Avatar asked Aug 28 '26 15:08

Antony


1 Answers

Something like this should work:

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!

like image 64
grtjn Avatar answered Aug 31 '26 08:08

grtjn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!