Nigel Alderton
Nigel Alderton

Reputation: 2376

Turn an XML sequence of attribute values into a string?

Using xquery 1.0, I want to take a sequence like this;

<foo xml:id="x20"/>
<foo xml:id="x47"/>
<foo xml:id="x3"/>

And collate the ids into a string to put in an attribute of a new element like this;

<bar ids="x20 x47 x3"/>

Is there a way to do it without manually iterating over the input sequence using a FLWOR?

Upvotes: 4

Views: 255

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243539

Use:

<bar ids="{/*/*/@xml:id}"/>

When this XQuery is applied on the following XML document (just wrapping the provided XML fragment into a well-formed XML document):

<t>
  <foo xml:id="x20"/>
  <foo xml:id="x47"/>
  <foo xml:id="x3"/>
</t>

the wanted, correct result is produced:

<bar ids="x20 x47 x3"/>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167696

let $s := (<foo xml:id="x20"/>,<foo xml:id="x47"/>,<foo xml:id="x3"/>)
return <bar ids="{$s/@xml:id}"/>

Upvotes: 5

Related Questions