我创建了一个 .xml 文件和一个模板来提取一些数据,但只显示属性。
这是我的 .xml-testfile:
<user id="1234" email="test.user@live.com" password="1234">
<type>Human</type>
<notes>
<note reference="5432" id="753" xmlns="http://testnamespace.de/note">
<text>example</text>
<username>John Doe</username>
<groups>
<group id="42">Avengers</group>
<group id="55">JLA</group>
</groups>
<distinctiveTitle>title</distinctiveTitle>
<personNameInverted>Doe John</personNameInverted>
</note>
</notes>
这里是相应的模板:
import module namespace tde = "http://marklogic.com/xdmp/tde" at "/MarkLogic/tde.xqy";
declare namespace testns = "http://testnamespace.de/note";
let $userNoteTDE:=
<template xmlns="http://marklogic.com/xdmp/tde" xmlns:testns="http://testnamespace.de/note">
<context>/user/notes/testns:note</context>
<rows>
<row>
<schema-name>user</schema-name>
<view-name>notes</view-name>
<columns>
<column>
<name>reference</name>
<scalar-type>string</scalar-type>
<val>@reference</val>
<nullable>true</nullable>
<default>""</default>
</column>
<column>
<name>id</name>
<scalar-type>string</scalar-type>
<val>@id</val>
<nullable>true</nullable>
<default>""</default>
</column>
<column>
<name>text</name>
<scalar-type>string</scalar-type>
<val>text</val>
<nullable>true</nullable>
<default>""</default>
</column>
<column>
<name>username</name>
<scalar-type>string</scalar-type>
<val>username</val>
<nullable>true</nullable>
<default>""</default>
</column>
<column>
<name>distinctiveTitle</name>
<scalar-type>string</scalar-type>
<val>distinctiveTitle</val>
<nullable>true</nullable>
<default>""</default>
</column>
<column>
<name>personNameInverted</name>
<scalar-type>string</scalar-type>
<val>personNameInverted</val>
<nullable>true</nullable>
<default>""</default>
</column>
</columns>
</row>
</rows>
</template>
我更改了上下文以使用正确的 (?) 路径和命名空间(因为这部分应该嵌套到另一个模板中):
<context>/user/notes/testns:note</context>
如果我用 检查模板tde:node-data-extract(fn:doc (TESTFILE PATH), $userNoteTDE)
我得到以下输出:
{
"TESTFILE PATH": [
{
"row": {
"schema": "user",
"view": "notes",
"data": {
"rownum": "1",
"reference": "5432",
"id": "753",
"text": "",
"username": "",
"distinctiveTitle": "",
"personNameInverted": ""
}
}
}
]
}
这表明,属性显示正确,但不知何故元素的值(文本、用户名、distinctTitle、personNameInverted)不起作用。
我的猜测是,这些值需要更精细的路径或表达式,但我找不到任何信息。
如果我更改 正文 值例如为
<val>testns:text</val>
在我的模板中,我收到错误: XDMP-UNBPRFX: (err:XPST0081) 前缀 testns 没有命名空间绑定(bind) 所以不知何故元素不能使用声明的命名空间,但属性可以。
我也跳过了
<groups>
我模板中的部分,因为他们需要自己的上下文,这应该无关紧要,是吗?在此先感谢您提供任何有用的见解!
最佳答案
MarkLogic Support 给了我这个问题的答案,所以我想在这里分享!
(谢谢克里斯哈姆林!)
这确实是一个命名空间问题。 MarkLogic Documentation表明对于多个命名空间,应该使用“路径命名空间”。
申报后...
<path-namespaces>
<path-namespace>
<prefix>testns</prefix>
<namespace-uri>http://testnamespace.de/note</namespace-uri>
</path-namespace>
</path-namespaces>
...
在模板和上下文之间,并使用 测试 在我的元素上添加前缀,例如 测试:文本 ,元素正在正确显示!
关于templates - MarkLogic 中的 XQuery 模板不显示任何值,只显示属性 (TDE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46317478/