MarkLogic javascript 如何获取元素节点的命名空间

问题描述 投票:0回答:1

我正在编写一个通用函数,尝试获取从

cts.search
函数返回的元素节点的命名空间。我面临的挑战是您无法获取元素的名称空间。我尝试使用
node.getAttributeNode("xmlns")
但这也不起作用。文档中好像没有支持javascript中的xml命名空间的函数

这是我正在使用的解决方案。


'use strict';

const DB_ID = xs.unsignedLong("5944350410045404402") //Meters database
const INVOKE_OPTS = {"database": xdmp.database()}
const getRoots = (bdone,iterations,filters,output)=>{
  let filterQuery = filters.map((filter)=>{
     return cts.notQuery(cts.documentRootQuery(filter))
  })
  let rootDoc = xdmp.invokeFunction(()=>fn.subsequence(cts.search(cts.andQuery([])),1,1),INVOKE_OPTS)
  let isEmpty = rootDoc != null ? true:false
  if(isEmpty != true) {
    return output
  } else {
      //Advance path the document node
      let node =  rootDoc.toArray()[0].xpath("/node()").toArray()[0]
      let nodeKind = node.nodeKind
      let baseUri = node.baseUri
      let nodeName = xs.QName("foo")
      let nodeNs = ""
      switch(nodeKind) {
        case "element" : 
          nodeName = node.nodeName
          nodeNs = node.getAttributeNode("xmlns")
          break;
        case "object-node" : 
        case "array-node" :
          nodeName = "json"
          break
      } 
      let nodePath = xdmp.path(node)
      return {
         "kind" : nodeKind,
         "ns" : nodeNs,
         "name" : nodeName,
          "path" : nodePath
      }
        
  }
  
}
getRoots(false,0,[],[])
javascript namespaces marklogic
1个回答
1
投票

XML 节点可以使用

namespaceURI

nodeNs = node.namespaceURI

您还可以使用 XPath 和

namespace-uri()
方法:

nodeNs = node.xpath("namespace-uri(.)")
© www.soinside.com 2019 - 2024. All rights reserved.