Marklogic - 如何隐藏元素节点
Marklogic - How to hide elements node
我尝试排除文档中不需要的节点。我不知道如何让它工作。
例如多个文档:
<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>
结果应该隐藏 "NonFundingSources" 元素和 return 所有其他元素节点。
<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 )
修剪元素的一种简单方法是应用一个 XSLT,该 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 security and redaction 功能,可用于隐藏或编辑内容。
我尝试排除文档中不需要的节点。我不知道如何让它工作。 例如多个文档:
<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>
结果应该隐藏 "NonFundingSources" 元素和 return 所有其他元素节点。
<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 )
修剪元素的一种简单方法是应用一个 XSLT,该 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 security and redaction 功能,可用于隐藏或编辑内容。