使用 xpath 将多个属性与节点值连接起来 [英] Join multiple concatenations of attributes with node values using xpath
问题描述
我有以下 xml 内容:
I have the following xml content:
<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>
我正在尝试使用 xpath 将其格式化并将其合并为一行.类似的东西:
I'm trying to format and combine this into one line using xpath. Something like:
string-join(//ns2:Creator/concat(./text(), @Role), ', ')
我想,我离某个地方很近,因为:
I think, i'm somewhere close, because this:
string-join(//ns2:Creator/@Role , ', ')
工作并给我一个逗号分隔的角色列表:Role1、Role2、Role3
works and gives me a comma-separated list of roles: Role1, Role2, Role3
还有这个
string-join(//ns2:Creator/node(), ', ')
结合创作者的价值观:创作者一、创作者二、创作者三".
combines the values of creators: "Creator One, Creator Two, Creator Three".
我想要
Role1: Creator One, Role2: Creator Two, Role3: Creator Three
你能帮忙吗.
推荐答案
您需要选择 @Role
作为调用 concat()
的第一个参数,并且使用.
选择上下文节点的字符串值:
You need to select @Role
as the first parameter to your call to concat()
, and use .
to select the string value of the context node:
string-join($xml//ns2:Creator/concat(@Role, ': ', .), ', ')
代替 .
您还可以使用 string()
或 string(.)
来明确转换,否则会隐式发生:
Instead of .
you can also use string()
or string(.)
which makes explicit the conversion that would otherwise happen implicitly:
string-join($xml//ns2:Creator/concat(@Role, ': ', string(.)), ', ')
返回:
Role1: Creator One, Role2: Creator Two, Role3: Creator Three
这篇关于使用 xpath 将多个属性与节点值连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!