首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MarkLogic 8 mem:节点-替换

MarkLogic 8 mem:节点-替换
EN

Stack Overflow用户
提问于 2015-10-16 13:24:38
回答 1查看 1.7K关注 0票数 0

我的数据包含了一些我需要保密的ID。我想使用带有私钥的散列函数,这样很好。但是当我试图替换内存中的节点时,我无法让它工作。

问题是:我将$values放在mem:node-replace中,但它不是一个节点,我尝试了xdmp:unquote和其他函数,但是它们没有帮助。

在这个示例中,我展示了$values,它是一个很好的“路径”,现在如何将它发送到节点?

代码语言:javascript
复制
$doc/*:source/*:content/cdm:BerichtInhoud/cdm:bericht[1]/cdm:id/text()

完整的例子:

代码语言:javascript
复制
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace mem = "http://xqdev.com/in-mem-update" at '/MarkLogic/appservices/utils/in-mem-update.xqy';
declare namespace cdm = "http://schemas.dikw.nl/cdm/1.2";

let $key := 'secretkey'
let $doc := 
  <source:source
      xmlns:source="http://marklogic.com/solutions/obi/source">
      <obi:metadata createdBy="admin" createdDateTime="2015-10-16T13:51:30.699172+02:00" lastUpdatedBy="admin" lastUpdatedDateTime="2015-10-16T13:51:30.699172+02:00"
          xmlns:obi="http://marklogic.com/solutions/obi">
      </obi:metadata>
      <source:id>5ac03a41-004a-459b-84b1-4efa8f193847</source:id>
      <dcterms:title
          xmlns:dcterms="http://purl.org/dc/terms/">dikw source data
      </dcterms:title>
      <source:content>
          <cdm:BerichtInhoud xsi:schemaLocation="http://schemas.dikw.nl/build22.xsd"
              xmlns="http://schemas.dikw.nl/export/1.0"
              xmlns:export="http://schemas.dikw.nl/exporter/1.0"
              xmlns:cdm="http://schemas.dikw.nl/cdm/1.2"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              <cdm:bericht>
                  <cdm:name>Thijs</cdm:name>
                  <cdm:id>12345</cdm:id>
              </cdm:bericht>
              <cdm:bericht>
                  <cdm:name>Piet</cdm:name>
                  <cdm:id>65756</cdm:id>
              </cdm:bericht>
              <cdm:bericht>
                  <cdm:name>Kees</cdm:name>
                  <cdm:id>87232</cdm:id>
              </cdm:bericht>
          </cdm:BerichtInhoud>
      </source:content>
  </source:source>    

for $singleid in $doc//cdm:id
  
  let $idstring := $singleid/text()

  let $sterk := substring(
    xdmp:hmac-sha512($key, $idstring, 'base64'),
    1, string-length($idstring))

  let $fullpath := xdmp:path($idstring)
  let $values   := fn:concat(fn:string('$doc'), $fullpath)
  let $nodev    := $values

  let $newdoc := mem:node-replace($idstring, <cdm:id>{$sterk}</cdm:id>)

return $values

编辑

在戴夫的博客上阅读和测试之后,这就是我想出来的。不幸的是,它并没有像我预期的那样起作用。代码不会在需要的地方更改元素。

问题是数据的格式给了local:change()函数。这是一份文件,这就是它失败的原因。我认为在我的项目的原始代码中,类型也是一个文档,但是我不能确定,因为xdmp:describe和xdmp:type不工作。

代码语言:javascript
复制
xquery version "1.0-ml";

import module namespace mem    = "http://xqdev.com/in-mem-update" at '/MarkLogic/appservices/utils/in-mem-update.xqy';
import module namespace functx = "http://www.functx.com" at "/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy";

declare namespace cdm = "http://schemas.dikw.nl/cdm/1.2";

declare function local:hashid($id) {
  let $_ := xdmp:log($id)
  let $key := 'secretkey'
  let $idstring := $id/text()

  let $sterk := substring(
    xdmp:hmac-sha512($key, $idstring, 'base64'),
    1, string-length($idstring))

  return $sterk
};

declare function local:change($node)
{
  typeswitch($node)
    (: case on id :)
    case element(cdm:id) return 
    element cdm:id {
      local:hashid($node)
    }
    (: default case, returns unchanged :)
    case element() return 
    element { fn:node-name($node) } {
      $node/@*,
      $node/node() ! local:change(.)
    }
  default return $node
};

declare variable $doc := 
  <source:source
      xmlns:source="http://marklogic.com/solutions/obi/source">
      <obi:metadata createdBy="admin" createdDateTime="2015-10-16T13:51:30.699172+02:00" lastUpdatedBy="admin" lastUpdatedDateTime="2015-10-16T13:51:30.699172+02:00"
          xmlns:obi="http://marklogic.com/solutions/obi">
      </obi:metadata>
      <source:id>5ac03a41-004a-459b-84b1-4efa8f193847</source:id>
      <dcterms:title
          xmlns:dcterms="http://purl.org/dc/terms/">dikw source data
      </dcterms:title>
      <source:content>
          <cdm:BerichtInhoud xsi:schemaLocation="http://schemas.dikw.nl/build22.xsd"
              xmlns="http://schemas.dikw.nl/export/1.0"
              xmlns:export="http://schemas.dikw.nl/exporter/1.0"
              xmlns:cdm="http://schemas.dikw.nl/cdm/1.2"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              <cdm:bericht>
                  <cdm:name>Thijs</cdm:name>
                  <cdm:id>12345</cdm:id>
              </cdm:bericht>
              <cdm:bericht>
                  <cdm:name>Piet</cdm:name>
                  <cdm:id>65756</cdm:id>
              </cdm:bericht>
              <cdm:bericht>
                  <cdm:name>Kees</cdm:name>
                  <cdm:id>87232</cdm:id>
              </cdm:bericht>
          </cdm:BerichtInhoud>
      </source:content>
  </source:source>;

return local:change(document {$doc})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-19 14:14:22

这是我最后在theGeert的帮助下提出的函数。棘手的部分是case document (),以解决传入数据是文档的问题。

代码语言:javascript
复制
declare function changeid(
  $document
){
  for $node in $document
    let $data :=
      typeswitch($node)
        (: case on the right cdm:id :)
        case element(cdm:id) return
        if ($node/parent::cdm:bericht) then
          element cdm:id {
            hashid($node)
          }
        else ()

        (: root level node :)
        case document-node() return
        document { $node/node() ! changeid(.) }

        (: default case, returns unchanged :)
        case element() return
        element { fn:node-name($node) } {
          $node/@*,
          $node/node() ! changeid(.)
        }
      default return ($node)
  return $data
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33171663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档