Join multiple concatenations of attributes with no

2019-08-05 07:57发布

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>

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 , ', ')

works and gives me a comma-separated list of roles: Role1, Role2, Role3

and this

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

combines the values of creators: "Creator One, Creator Two, Creator Three".

I'd like the final output of

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

Could you please help.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-05 08:31

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, ': ', .), ', ')

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(.)), ', ')

Returns:

Role1: Creator One, Role2: Creator Two, Role3: Creator Three
查看更多
登录 后发表回答