首页 文章

xquery:如何在xml节点中删除未使用的命名空间?

提问于
浏览
1

我有一个xml文档相同:

<otx xmlns="http://iso.org/OTX/1.0.0" xmlns:i18n="http://iso.org/OTX/1.0.0/i18n" xmlns:diag="http://iso.org/OTX/1.0.0/DiagCom" xmlns:measure="http://iso.org/OTX/1.0.0/Measure" xmlns:string="http://iso.org/OTX/1.0.0/StringUtil" xmlns:dmd="http://iso.org/OTX/1.0.0/Auxiliaries/DiagMetaData" xmlns:fileXml="http://vwag.de/OTX/1.0.0/XmlFile" xmlns:log="http://iso.org/OTX/1.0.0/Logging" xmlns:file="http://vwag.de/OTX/1.0.0/File" xmlns:dataPlus="http://iso.org/OTX/1.0.0/DiagDataBrowsingPlus" xmlns:event="http://iso.org/OTX/1.0.0/Event" xmlns:quant="http://iso.org/OTX/1.0.0/Quantities" xmlns:hmi="http://iso.org/OTX/1.0.0/HMI" xmlns:math="http://iso.org/OTX/1.0.0/Math" xmlns:flash="http://iso.org/OTX/1.0.0/Flash" xmlns:data="http://iso.org/OTX/1.0.0/DiagDataBrowsing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dt="http://iso.org/OTX/1.0.0/DateTime" xmlns:eventPlus="http://iso.org/OTX/1.0.0/EventPlus" xmlns:corePlus="http://iso.org/OTX/1.0.0/CorePlus" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:job="http://iso.org/OTX/1.0.0/Job" id="id_4e5722f2f81a4309860c146fd3c743e5" name="NewDocument1" package="NewOtxProject1Package1" version="1.0.0.0" timestamp="2014-04-11T09:42:50.2091628+07:00">
  <declarations>
    <constant id="id_fdc639e20fbb42a4b6b039f4262a0001" name="GlobalConstant">
      <realisation>
        <dataType xsi:type="Boolean">
          <init value="false"/>
        </dataType>
      </realisation>
    </constant>
  </declarations>
  <metaData>
    <data key="MadeWith">Created by emotive Open Test Framework - www.emotive.de</data>
    <data key="OtfVersion">4.1.0.8044</data>
  </metaData>
  <procedures>
    <procedure id="id_1a80900324c64ee883d9c12e08c8c460" name="main" visibility="PUBLIC">
      <realisation>
        <flow/>
      </realisation>
    </procedure>
  </procedures>
</otx>

我想通过xquery删除xml中未使用的命名空间,但我不知道解决它的任何解决方案 . 使用的当前命名空间是“http://iso.org/OTX/1.0.0http://www.w3.org/2001/XMLSchema-instance” .
请帮我! .

1 回答

  • 2

    不幸的是,XQuery Update没有提供表达式来删除现有的命名空间,但是你可以递归地重建你的文档:

    declare function local:strip-ns($n as node()) as node() {
      if($n instance of element()) then (
        element { node-name($n) } {
          $n/@*,
          $n/node()/local:strip-ns(.)
        }
      ) else if($n instance of document-node()) then (
        document { local:strip-ns($n/node()) }
      ) else (
        $n
      )
    };
    
    let $xml :=
      <A xmlns="used1" xmlns:unused="unused">
        <b:B xmlns:b="used2"/>
      </A>
    return local:strip-ns($xml)
    

相关问题