gpt4 book ai didi

xml - 使用 Teiid 中的 xpath 连接多个属性与节点值的串联

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:54 28 4
gpt4 key购买 nike

这是我发布的问题的副本 here我只需要注意这发生在 teiid 引擎的 XMLTABLE 中。我正在解析以下 xml 内容:

<AttributeSets>
<ns2:ItemAttributes xml:lang="de-DE">
<ns2:Creator Role="Role1">Creator One</ns2:Creator>
<ns2:Creator Role="Role2">Creator Two</ns2:Creator>
<ns2:Creator Role="Role3">Creator Three</ns2:Creator>
</ns2:ItemAttributes>
</AttributeSets>

使用

    Select * From
XMLTABLE(XMLNAMESPACES( DEFAULT 'http://ns1',
'http://ns2/default.xsd' as ns2 ),
'AttributeSets/ns2:ItemAttributes' PASSING x.AttributeSets
COLUMNS
Creator STRING PATH 'string-join(//ns2:Creator/(@Role, node()) , '', '')'
) p;

这里的 x.AttributeSets 是一个 xml 类型的变量,包含上面的内容。

我正在尝试使用 xpath 将其格式化并合并为一行。像这样的东西:

string-join(//ns2:Creator/concat(./text(), @Role), ', ')

我想,我离得很近,因为:

string-join(//ns2:Creator/@Role , ', ')

工作并给我一个逗号分隔的角色列表:Role1、Role2、Role3

还有这个

string-join(//ns2:Creator/node(), ', ')

结合了创作者的值(value)观:“创作者一号、创作者二号、创作者三号”。

我想要的最终输出

Role1: Creator One, Role2: Creator Two, Role3: Creator Three

我最多能得到的是:

 string-join(//ns2:Creator/(@Role, node()) , ', ')

此逗号将所有角色和创建者分隔成一行。由于某种原因,concat 运算符似乎不起作用。你能帮忙吗?

最佳答案

试试下面的xpath

string-join(for $n in /AttributeSets/ItemAttributes/Creator return concat($n/@Role, ':',$n/text()),',')

请记住根据您的源代码树上下文调整 for 中的选择器 xpath。因此我假设了文档根目录

/AttributeSets/ItemAttributes/Creator

string-join 函数的文档位于 W3C有一个非常相似的例子。

更新:

I think, i'm somewhere close, because this: ... works and gives me a comma-separated list of roles: Role1, Role2, Role3

认为你们很亲密。我尝试对此进行了一些小调整,这很有效:

string-join(//ns2:Creator/concat(@Role, ':', ./text()), ', ')

For some reason the concat operator seems no to work.

不确定,我用在线 xpath 测试器检查过 here它完美地工作。事实上,在线工具也接受您的 xapth,输出如下:

Creator OneRole1, Creator TwoRole2, Creator ThreeRole3

关于xml - 使用 Teiid 中的 xpath 连接多个属性与节点值的串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40534982/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com