1

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.

Eugene Bykov
  • 133
  • 8
  • Getting a bit closer using: string-join(//ns2:Creator/(@Role, node()) , ', ') Now I have all values, but not that pretty - they are all comma-separated – Eugene Bykov Nov 10 '16 at 17:27

1 Answers1

0

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
wst
  • 11,681
  • 1
  • 24
  • 39
  • Thanks, but for some reason in this case I don't get values. The output becomes: ": , : " So it catches the two items, the colon with a whitespace, but gets no values – Eugene Bykov Nov 10 '16 at 17:38
  • @EugeneBykov Then I suspect you have changed something about either the query or the data, because I have tested this on the same data you provide in your question and the output is correct. – wst Nov 10 '16 at 17:40
  • This is inside teiid sql, may be this matters... The concat processing for example... – Eugene Bykov Nov 10 '16 at 17:42
  • @EugeneBykov That seems likely. However, this is the answer to your question about XQuery. If you have a question about Teiid, you should ask a new question. – wst Nov 10 '16 at 17:45
  • Continued here http://stackoverflow.com/questions/40534982/join-multiple-concatenations-of-attributes-with-node-values-using-xpath-in-teiid – Eugene Bykov Nov 10 '16 at 18:53