xpath - how to iterate dynamically over child elements using for in xquery -
i have xml file contains multiple xml documents. in large xml document each document has root node , common xml documents in file. child elements inside root name vary. have write loop iterate dynamically each , every child node , have show on table format. here have pasted xml format below. have multiple xml files in way root node common every file. want map each child element table row , data table column. child elements numbers vary
<source> <empname>john </empname> <empid>25825 </empid> <salary> 20000 </salary> <dob> 12-08-1993</dob> </source> <source> <empname>joe</empname> <empid>25826</empid> <salary>20000</salary> <dob>12-07-1993</dob> <source> <source> <emptype>developer</emptype> <address>3155 </address> <mobile>58258365</mobile> </source> <source> <emptype>analyst</emptype> <address>3155 </address> <mobile>58258365</mobile> </source> <table> <th> empname empid salary dob </th> <td> john 25825 60000 date</td> <td> joe 25826 70000 date</td> </table>
every time <th>
, <td>
changed , common thing root element source code. can me out iterate elements on loop , represent data on table format?
you try this, approach of distinct-values doesn't scale well. best determine columns upfront, , hardcode solution tavolo:
let $xml := <sources> <source> <empname>john </empname> <empid>25825 </empid> <salary> 20000 </salary> <dob> 12-08-1993</dob> </source> <source> <empname>joe</empname> <empid>25826</empid> <salary>20000</salary> <dob>12-07-1993</dob> </source> <source> <emptype>developer</emptype> <address>3155 </address> <mobile>58258365</mobile> </source> <source> <emptype>analyst</emptype> <address>3155 </address> <mobile>58258365</mobile> </source> </sources> let $sources := $xml/source let $labels := fn:distinct-values($sources/*/fn:node-name(.)) return <table> <tr>{ $label in $labels return <th>{ $label }</th> }</tr> { $source in $sources return <tr>{ $label in $labels return <th>{ fn:data($source/*[fn:node-name() = $label]) }</th> }</tr> } </table>
hth!