问题
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">
回答1:
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.
回答2:
//Section/@name
or
//Section/@name/string(.)
for the string values
回答3:
/Section/@name
来源:https://stackoverflow.com/questions/324289/xquery-to-get-list-of-attributes