xslt - Created nested xml from flat xml -


okay , stuck.

i'm trying recreate hierarchy flat xml file , don't know how proceed. xml file i'm working looks bit this:

<table name="ecatalogue">   <tuple>     <atom name="irn">2470</atom>     <atom name="eadunitid"></atom>     <atom name="eadunittitle"></atom>     <atom name="eadlevelattribute"></atom>     <tuple name="assparentobjectref">     </tuple>   </tuple>    <tuple>     <atom name="irn">5416</atom>     <atom name="eadunitid"></atom>     <atom name="eadunittitle"></atom>     <tuple name="assparentobjectref">       <atom name="irn">2470</atom>       <atom name="eadunittitle"></atom>     </tuple>   </tuple>    <tuple>     <atom name="irn">7</atom>     <atom name="eadunitid"></atom>     <atom name="eadunittitle"></atom>     <tuple name="assparentobjectref">       <atom name="irn">2470</atom>       <atom name="eadunittitle"></atom>     </tuple>   </tuple>    <tuple>     <atom name="irn">8</atom>     <atom name="objecttype"></atom>     <atom name="eadlevelattribute"></atom>     <atom name="eadunitid"></atom>     <atom name="eadunittitle"></atom>     <tuple name="assparentobjectref">       <atom name="eadunittitle"></atom>       <atom name="irn">7</atom>     </tuple>   </tuple> </table> 

what achieve file looks this:

<table name="ecatalogue">     <collection>        <tuple>          <atom name="irn">2470</atom>          <atom name="eadunitid"></atom>          <atom name="eadunittitle"></atom>          <atom name="eadlevelattribute"></atom>          <tuple name="children">             <tuple>                  <atom name="irn">5416</atom>                 <atom name="eadunitid"></atom>                 <atom name="eadunittitle"></atom>                 <atom name="eadlevelattribute"></atom>                 <tuple name="children"></tuple>             </tuple>             <tuple>                  <atom name="irn">7</atom>                 <atom name="eadunitid"></atom>                 <atom name="eadunittitle"></atom>                 <atom name="eadlevelattribute"></atom>                 <tuple name="children">                     <tuple>                          <atom name="irn">8</atom>                         <atom name="eadunitid"></atom>                         <atom name="eadunittitle"></atom>                         <atom name="eadlevelattribute"></atom>                         <tuple name="children"></tuple>                     </tuple>                 </tuple>             </tuple>         </tuple>       </tuple>     </collection> 

the challenges here there can number of top level items , number of child records. also, can extend multiple levels - don't know how deep child records extend to. way of finding out relationship between records

<tuple name="assparentobjectref"> 

which gives immediate parent record , empty if record has no parent. if use xslt, limited xslt 1.0.

given in xml thought xslt might way forward, know next nothing xslt , attempts far have resulted in nothing. have read on muenchian method , thought might help, below example of have tried far (which fair indication of how of novice @ xslt :-) ):

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"   version="1.0">    <xsl:output indent="yes"/>    <xsl:key name="parent-irn" match="tuple[@name='assparentobjectref']" use="atom[@name='irn']" />    <xsl:template match="/table/tuple">     <xsl:copy>       <xsl:apply-templates select="*"/>     </xsl:copy>   </xsl:template>    <xsl:template match="/table/tuple">     <xsl:if test="generate-id() = generate-id(key('parent-irn', atom[@name='irn'])[1])">       <collection>         <xsl:attribute name="title">           <xsl:value-of select="tuple[@name='assparentobjectref']"/>         </xsl:attribute>         <xsl:for-each select="key('parent-irn', atom[@name='irn'])">           <tuple>             <xsl:copy-of select="atom[@name='eadunittitle']" />             <xsl:copy-of select="atom[@name='eadunitid']" />             <xsl:copy-of select="atom[@name='eadlevelattribute']" />           </tuple>         </xsl:for-each>       </collection>     </xsl:if>   </xsl:template>  </xsl:stylesheet>  

am on right track this? xslt right tool job?

edit added missing 'tuple' tag desired output.

you should try way:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:key name="child" match="tuple" use="tuple[@name='assparentobjectref']/atom[@name='irn']" />  <xsl:template match="/table">     <table name="ecatalogue">         <collection>             <xsl:apply-templates select="tuple[not(tuple[@name='assparentobjectref']/atom[@name='irn'])]"/>         </collection>     </table> </xsl:template>  <xsl:template match="tuple">     <tuple>         <xsl:copy-of select="atom"/>         <xsl:if test="key('child', atom[@name='irn'])">             <tuple name="children">                 <xsl:apply-templates select="key('child', atom[@name='irn'])"/>              </tuple>         </xsl:if>     </tuple> </xsl:template>  </xsl:stylesheet> 

the result here different 1 posted:

<?xml version="1.0" encoding="utf-8"?> <table name="ecatalogue">    <collection>       <tuple>          <atom name="irn">2470</atom>          <atom name="eadunitid"/>          <atom name="eadunittitle"/>          <atom name="eadlevelattribute"/>          <tuple name="children">             <tuple>                <atom name="irn">5416</atom>                <atom name="eadunitid"/>                <atom name="eadunittitle"/>             </tuple>             <tuple>                <atom name="irn">7</atom>                <atom name="eadunitid"/>                <atom name="eadunittitle"/>                <tuple name="children">                   <tuple>                      <atom name="irn">8</atom>                      <atom name="objecttype"/>                      <atom name="eadlevelattribute"/>                      <atom name="eadunitid"/>                      <atom name="eadunittitle"/>                   </tuple>                </tuple>             </tuple>          </tuple>       </tuple>    </collection> </table> 

but not sure correct result because example ambiguous.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -