Marklogic - 如何隐藏元素节点

时间:2018-04-09 17:38:57

标签: marklogic

我尝试在文档中排除不需要的节点。我无法弄清楚如何让它起作用。 例如多个文件:

<doc:Record Type="testdata" xmlns:doc="http://db/test/record">
  <meta:Metadata xmlns:meta="http://db/test/record/meta">

    <meta:docid>09266</meta:docid>
    <meta:Collections>
       <meta:Collection>Universities</meta:Collection>
    </meta:Collections>
    <meta:NonFundingSources>
      <meta:TotalNonDODFunding>0 </meta:TotalNonDODFunding>
    </meta:NonFundingSources>
  </meta:Metadata>
</doc:Record>

结果应隐藏&#34; NonFundingSources&#34;元素并返回所有其他元素节点。

 <doc:Record Type="testdata" xmlns:doc="http://db/test/record">
   <meta:Metadata xmlns:meta="http://db/test/record/meta">

     <meta:docid>09266</meta:docid>
     <meta:Collections>
       <meta:Collection>Universities</meta:Collection>
     </meta:Collections>
  </meta:Metadata>
</doc:Record>

在我的代码中,我检索了所有元素,但在保存到文件系统之前,我无法弄清楚如何隐藏(删除)不需要的元素节点。

let $uris := cts:uris((),
                  (),
                  cts:and-query((
                          cts:collection-query("/ure/univs"),
                          cts:field-word-query("dc",("a","be","x")),
                          cts:field-range-query("crd",">=","2011-01-01"),
                          cts:field-range-query("crd","<","2012-01-01")
                  ))

                 )

   for $uri in $uris

   let $docNumber := fn:data($uri//meta:docid)

   return xdmp:save(fn:concat("/report/",$docNumber,".xml") ,$uri ) 

1 个答案:

答案 0 :(得分:3)

修剪元素的一种简单方法是应用一个XSLT,它使用identity template复制大多数内容,并在要删除的元素上匹配一个空模板:

declare variable $XSLT := 
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:meta="http://db/test/record/meta">
    <xsl:output indent="yes"/>
    <!--by default, every attribute and node is copied to the output-->
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
    </xsl:template>
    <!--Whenever this element is matched, no output is generated for it, 
        or any of it's descendants -->
    <xsl:template match="meta:NonFundingSources"/>
</xsl:stylesheet>;

for $doc in cts:search(doc(),  
                        cts:and-query((
                          cts:collection-query("/ure/univs"),
                          cts:field-word-query("dc",("a","be","x")),
                          cts:field-range-query("crd",">=","2011-01-01"),
                          cts:field-range-query("crd","<","2012-01-01")
                        ))
              )

let $docNumber := $doc//meta:docid/string()
(: transform the document, removing content that we don't want :)
let $redacted := xdmp:xslt-eval($XSLT, $doc)
return 
  xdmp:save(fn:concat("/report/",$docNumber,".xml"), $redacted )

MarkLogic还具有element level securityredaction功能,可用于隐藏或编辑内容。