Showing posts with label xpath. Show all posts
Showing posts with label xpath. Show all posts

Monday, 4 January 2016

Determine the position and content of an item in a string sequence

Issue

We have a sequence of strings and for each item we need the position of the item within the sequence

Resolution

use the following xpath expression

Xquery

let
  $items := tokenize('xslt-tricks.blogspot.co.uk/search/label/xquery','/')
return
  for $i in 1 to count($items) 
  return (
   $i, (: position  :)
   $items[$i]  (: item content :)
  )

Wednesday, 25 June 2014

Xpath to generate an xpath string to the current item

An xpath to be used in either xquery or XSLT to generate the heirarchical path to the current item


string-join(
   (for $node in ancestor::* 
    return 
      concat($node/name(),
              '[', 
              count($node/preceding-sibling::*[name() = $node/name()])+1, 
              ']'
            ),
      concat(name(),
              '[', 
              count(preceding-sibling::*[name() = current()/name()]) + 1, 
              ']'
            )
    ),
  '/')