如何在JavaScript + Marklogic中声明xml命名空间

How to declare xml namespace in JavaScript + Marklogic

本文关键字:声明 xml 命名空间 Marklogic JavaScript      更新时间:2023-09-26

我有像-

这样的xml文档
<domain xmlns:c="http://example.com/ns/core" xmlns="http://example.com/ns/core">
  <c:id>http://example.com/xyz/no-data</c:id>
</domain>

我在MarkLogic中使用JavaScript,并希望在c:id上运行元素值查询。像这样-

cts.elementValueQuery(xs.QName("c:id"), "http://example.com/xyz/no-data")

但是为此我需要声明命名空间c。如果是xQuery的话我们可以这样做-

declare namespace c="http://example.com/ns/core";

但是我不知道如何在JavaScript中做到这一点

您可以使用fn.QName()代替xs.QName()。在下面的示例中,我将nsC(名称空间- c)声明为类似于声明的名称空间前缀的东西。

const nsC = "http://example.com/ns/core";
cts.elementValueQuery(
  fn.QName(nsC, "id"), 
  "http://example.com/xyz/no-data"
)