xml - XQuery or XPath expression explained -


could break down xquery doing?

'for $i in . return count(../*[. << $i])'

i can tell loop, confused ../* , []. in sql code i'm working on. trying extract column-name nodes xml file:

    select distinct      parent.items.value('for $i in . return count(../*[. << $i])', 'int') [index],     parent.items.value('local-name(../.)', 'varchar(100)') 'parentitem',     parent.items.value('local-name(.)', 'varchar(100)') 'childitem'        dbo.myformresults     cross apply xmlformfields.nodes('/form/*')  parent(items) 

i want xquery iterate through xml nodes in order in xml file. sort of works, double counts , i'm not sure why... maybe if understand loop better can fix problem.

enter image description here

as can see here indexes: 15, 16 , 17 duplicated.

thanks!

for $i in . 

a for $x in y return z expression assigns variable $x each item in sequence y in turn , evaluates expression z, returning sequence concatenation of results evaluations of z, e.g. for $i in (1, 2, 3) return (2 * $i) generate sequence (2, 4, 6).

in case sequence y single node - it's idiom capture current value of . in variable can make use of in deeper predicates, alternative xslt current() function when you're working xpath 2.0 outside of xslt.

../*[. << $i] 

the << operator tests relative position of nodes, x << y true if x comes before y in document order.

therefore for $i in . return count(../*[. << $i]) long-winded way of writing count(preceding-sibling::*) - number of sibling elements of current node (., known $y) come before node in document order.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -