Skip to content

Retrieving the first node from an XML descendant list

by Cory Foy on August 1st, 2006

File under “Things I’ll go searching for in about 2 years and won’t be able to find”

We’ve been doing a lot of crazy things with Oracle at work. Our team has been using some Advanced Queuing features to pass XML data down, and then shredding the XML into tables. It’s great fun. Today we needed to grab the first occurrence of a node and get its value. In other words, we had an XML document like:



  
    2006
  

  
    2006
  


and we wanted to write an XPath expression to retrieve 2006, since it’s just duplicated data. One might think that it would be as simple as //year[1] or //year[position()=1] but alas, the // returns a context-aware node-set, so both nodes are considered to be at position 1, therefore they get returned concatenated.

After banging our heads for about 2 hours, we stumbled upon this just before we went home: (//year)[1]. Our guess is that putting the parenthesis around it turned it into a regular node-set, allowing us to then get the node at the first position.

From → Uncategorized

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS