views:

2017

answers:

3

If I have several Section elements in an XML document, what XQuery do I use to get a list of all the name values?

<Section name="New Clients" filePath="XNEWCUST.TXT" skipSection="False">
A: 
 /Section/@name
James Curran
This will select the "name" attribute node of the only top element of the XML document, if the top element happens to be a "Section" element. What the OP wanted is the sequence of the *string values* of the "name" attributes of *all* "Section" elements in the XML document.
Dimitre Novatchev
+1  A: 

In XPath 2.0 (which is a subset of XQuery) one would use the following expression to get a sequence of all string values of the "name" attributes of the "Section" elements:

     for $attr in //Section/@name
              return string($attr)

Do note that using the "//" abbreviation is typically a bad practice as this may require a whole (subtree) to be traversed. In any case where the structure of the document is known a more specific XPath expression (such as one using specific location steps) should be preferred.

Dimitre Novatchev
Thanks, all I needed was a return concat(string($attr), "-") to delimit the values.
ProfK
A: 
//Section/@name

or

//Section/@name/string(.)

for the string values

Oliver Hallam