xml - Confusion regarding descendant axis and '//' -


structure of document:

<program>  <projectionday>    <projection/>    <projection/>  </projectionday>  <projectionday>    <projection/>    <projection/>  </projectionday> </program> 

i want select first , last projection ( across whole document).

this returns it:

/descendant::projection[position() = 1 or position() = last()] 

this returns first , last within projectionday

//projection[position() = 1 or position() = last()] 

why so?

your first query using descendant fetches <projection/> elements, filters result set first , last element:

/descendant::projection[position() = 1 or position() = last()] 

// abbreviation /descendant-or-self::*/. second query means

/descendant-or-self::*/projection[position() = 1 or position() = last()] 

which looks elements (here: each <projectionday/>, , returns first , last <projection/> element inside element.


to return first , last element on <projeectionday/>s, put before predicate parentheses:

(/descendant-or-self::*/projection)[position() = 1 or position() = last()] 

or abbreviated:

(//projection)[position() = 1 or position() = last()] 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -