Word.CustomXMLPart API 中的 `namespaceMappings` 是什么意思
What does `namespaceMappings` mean in Word.CustomXMLPart API
我在 Script Lab 中创建了一个小片段,它基本上是:
- 创建自定义 XML:
<AP xmlns="accordproject.org">
<template xmlns="acceptance-of-delivery">
<shipper>Aman Sharma</shipper>
</template>
</AP>
- 尝试使用 xPath
/AP/template
查询此 xml。我运行这段代码:
await Word.run(async context => {
const customXmlParts = context.document.customXmlParts;
const AP = customXmlParts.getByNamespace("accordproject.org").getOnlyItemOrNullObject();
await context.sync();
const nodes = AP.query('/AP/template', {}); // how does this work?
await context.sync();
console.log(nodes);
})
- 删除自定义XML。
query
API的第二个参数是namespaceMappings
。我想我传递的不正确,这就是为什么我将其作为输出(空对象)的原因。
但是当我传递 *
而不是 /AP/template
时,我得到了整个 XML(而第二个参数 namespaceMappings
保持不变)。
我哪里错了?任何人都可以分享一些片段来帮助我查询 customXML.
简短的回答是您可以使用
const nodes = AP.query("/n1:AP/n2:template", {n1:"accordproject.org", n2:"acceptance-of-delivery"});
我根本不知道 JS/TS 但我认为这些基本上是某种键值对。您也可以使用
const nodes = AP.query("/n1:AP/n2:template", {"n1":"accordproject.org", "n2":"acceptance-of-delivery"});
如果您更愿意将命名空间前缀视为字符串。
(对于不熟悉的人,“n1”和“n2”只是您发明的前缀,因此您可以引用完整的命名空间 URI。它们与您可能在本文中使用的任何前缀没有任何关系XML 个您正在查询。)
我也找不到这方面的文档,最初假设您可能需要更多类似 { Prefix:"ns1", namespaceURI:"the namespace URI" } 的东西,但这只是因为那些是 属性 VBA 模型中使用的名称。
我在 Script Lab 中创建了一个小片段,它基本上是:
- 创建自定义 XML:
<AP xmlns="accordproject.org">
<template xmlns="acceptance-of-delivery">
<shipper>Aman Sharma</shipper>
</template>
</AP>
- 尝试使用 xPath
/AP/template
查询此 xml。我运行这段代码:
await Word.run(async context => {
const customXmlParts = context.document.customXmlParts;
const AP = customXmlParts.getByNamespace("accordproject.org").getOnlyItemOrNullObject();
await context.sync();
const nodes = AP.query('/AP/template', {}); // how does this work?
await context.sync();
console.log(nodes);
})
- 删除自定义XML。
query
API的第二个参数是namespaceMappings
。我想我传递的不正确,这就是为什么我将其作为输出(空对象)的原因。
但是当我传递 *
而不是 /AP/template
时,我得到了整个 XML(而第二个参数 namespaceMappings
保持不变)。
我哪里错了?任何人都可以分享一些片段来帮助我查询 customXML.
简短的回答是您可以使用
const nodes = AP.query("/n1:AP/n2:template", {n1:"accordproject.org", n2:"acceptance-of-delivery"});
我根本不知道 JS/TS 但我认为这些基本上是某种键值对。您也可以使用
const nodes = AP.query("/n1:AP/n2:template", {"n1":"accordproject.org", "n2":"acceptance-of-delivery"});
如果您更愿意将命名空间前缀视为字符串。
(对于不熟悉的人,“n1”和“n2”只是您发明的前缀,因此您可以引用完整的命名空间 URI。它们与您可能在本文中使用的任何前缀没有任何关系XML 个您正在查询。)
我也找不到这方面的文档,最初假设您可能需要更多类似 { Prefix:"ns1", namespaceURI:"the namespace URI" } 的东西,但这只是因为那些是 属性 VBA 模型中使用的名称。