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

Join multiple concatenations of attributes with node values using xpath In Teiid

这是我发布的问题的副本 我只需要注意这发生在 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(), ', ')

结合创作者的价值观:"Creator One, Creator Two, Creator Three".

我想要

的最终输出
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