xml - Adding elements to tag when conditions match in a loop using XSLT -


i'm trying achieve in xslt 1.0 , below conditions @ point of time occurrences of adt more occurrences of inf 1 adt should have 1 surname,firstname , dob details of inf tag if adt tags more inf tags first adt tags should tagged inf , rest should adt tags should not have inf attached it. no change child tag required should present in output.

i'm trying achieve below output using xslt couldn't fix it, in appreciated.

need in below xslt transformation

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:template match="/">         <travelergroup>             <xsl:for-each select="/travelergroup/traveler">                 <xsl:if test="travelerinfo/travelertypecode != 'inf' ">                     <traveler>                         <travelerinfo>                             <elementnumber>                                 <xsl:value-of select="travelerinfo/elementnumber" />                             </elementnumber>                             <travelertypecode>                                 <xsl:value-of select="travelerinfo/travelertypecode" />                             </travelertypecode>                             <travelerdetails>                                 <firstname>                                     <xsl:value-of select="concat(travelerinfo/travelerdetails/firstname" />                                 </firstname>                                 <surname>                                     <xsl:value-of select="travelerinfo/travelerdetails/surname" />                                 </surname>                                 <xsl:if test="travelerinfo/travelertypecode = 'chd' ">                                     <dateofbirth>                                         <xsl:value-of select="travelerinfo/travelerdetails/dateofbirth" />                                     </dateofbirth>                                 </xsl:if>                             </travelerdetails>                             <xsl:if test="travelerinfo/travelertypecode != 'chd' ">                                 <xsl:variable name="i" select="position()" />                                 <xsl:for-each select="/traveler[travelerinfo/travelertypecode = 'inf'][$i]">                                     <xsl:if test="travelerinfo/travelertypecode = 'inf' ">                                         <infant>                                             <surname>                                                 <xsl:value-of select="travelerinfo/travelerdetails/surname" />                                             </surname>                                             <firstname>                                                 <xsl:value-of select="travelerinfo/travelerdetails/firstname" />                                             </firstname>                                             <dateofbirth>                                                 <xsl:value-of select="travelerinfo/travelerdetails/dateofbirth" />                                             </dateofbirth>                                         </infant>                                     </xsl:if>                                 </xsl:for-each>                             </xsl:if>                         </travelerinfo>                     </traveler>                 </xsl:if>             </xsl:for-each>         </travelergroup>     </xsl:template> </xsl:stylesheet> 

input xml :

<travelergroup> <traveler>     <travelerinfo>         <elementnumber>1</elementnumber>         <travelertypecode>adt</travelertypecode>         <travelerdetails>             <firstname>adult one</firstname>             <surname>surname</surname>         </travelerdetails>     </travelerinfo> </traveler> <traveler>     <travelerinfo>         <elementnumber>2</elementnumber>         <travelertypecode>adt</travelertypecode>         <travelerdetails>             <firstname>adult two</firstname>             <surname>surname</surname>         </travelerdetails>     </travelerinfo> </traveler> <traveler>     <travelerinfo>         <travelertypecode>inf</travelertypecode>         <elementnumber>3</elementnumber>         <travelerdetails>             <firstname>infant one</firstname>             <surname>surname</surname>             <dateofbirth>2016-01-06</dateofbirth>         </travelerdetails>     </travelerinfo> </traveler> <traveler>     <travelerinfo>         <travelertypecode>inf</travelertypecode>         <elementnumber>4</elementnumber>         <travelerdetails>             <firstname>infant two</firstname>             <surname>surname</surname>             <dateofbirth>2017-01-06</dateofbirth>         </travelerdetails>     </travelerinfo> </traveler> <traveler>     <travelerinfo>         <elementnumber>5</elementnumber>         <travelertypecode>adt</travelertypecode>         <travelerdetails>             <firstname>adult three</firstname>             <surname>surname</surname>         </travelerdetails>     </travelerinfo> </traveler> <traveler>     <travelerinfo>         <elementnumber>6</elementnumber>         <travelertypecode>chd</travelertypecode>         <travelerdetails>             <firstname>child one</firstname>             <surname>surname</surname>             <dateofbirth>2013-01-06</dateofbirth>         </travelerdetails>     </travelerinfo> </traveler> </travelergroup> 

expected output :

<travelergroup> <traveler>     <elementnumber>1</elementnumber>     <travelertypecode>adt</travelertypecode>     <travelerdetails>         <firstname>adult one</firstname>         <surname>surname</surname>         <middlename />     </travelerdetails>     <infant>         <surname>infant one</surname>         <firstname>surname</firstname>         <dateofbirth>2016-01-06</dateofbirth>     </infant> </traveler> <traveler>     <elementnumber>2</elementnumber>     <travelertypecode>adt</travelertypecode>     <travelerdetails>         <firstname>adult two</firstname>         <surname>surname</surname>         <middlename />     </travelerdetails>     <infant>         <surname>infant two</surname>         <firstname>surname</firstname>         <dateofbirth>2017-01-06</dateofbirth>     </infant> </traveler> <traveler>     <elementnumber>5</elementnumber>     <travelertypecode>adt</travelertypecode>     <travelerdetails>         <firstname>adult three</firstname>         <surname>surname</surname>         <middlename />     </travelerdetails> </traveler>  <traveler>     <elementnumber>6</elementnumber>     <travelertypecode>chd</travelertypecode>     <travelerdetails>         <firstname>child one</firstname>         <surname>surname</surname>         <dateofbirth>2013-01-06</dateofbirth>     </travelerdetails> </traveler>   </travelergroup>    

there couple of minor glitches in output. values of <firstname> , <surname> inf interchanged. <middlename> adult not present in input shown in output. if need <middlename> in output, can make necessary changes below xsl.

xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="xml" indent="yes" />     <xsl:strip-space elements="*" />      <xsl:template match="travelergroup">         <xsl:copy>             <!-- loop through adults -->             <xsl:for-each select="traveler/travelerinfo[travelertypecode = 'adt']">                 <traveler>                     <xsl:variable name="adtindex" select="position()" />                     <!-- copy nodes adult -->                     <xsl:copy-of select="@* | node()" />                      <!-- loop through infants -->                     <xsl:for-each select="//traveler/travelerinfo[travelertypecode = 'inf']">                         <xsl:variable name="infindex" select="position()" />                          <!-- compare adult node index infant node index -->                         <xsl:if test="$adtindex = $infindex">                             <infant>                                 <!-- copy infant nodes within adult -->                                 <xsl:copy-of select="travelerdetails/firstname" />                                 <xsl:copy-of select="travelerdetails/surname" />                                 <xsl:copy-of select="travelerdetails/dateofbirth" />                             </infant>                         </xsl:if>                     </xsl:for-each>                 </traveler>             </xsl:for-each>             <!-- loop through child , copy them -->             <xsl:for-each select="//traveler/travelerinfo[travelertypecode = 'chd']" >                 <traveler>                     <xsl:copy-of select="@* | node()" />                 </traveler>             </xsl:for-each>         </xsl:copy>     </xsl:template> </xsl:stylesheet> 

output

<travelergroup>     <traveler>         <elementnumber>1</elementnumber>         <travelertypecode>adt</travelertypecode>         <travelerdetails>             <firstname>adult one</firstname>             <surname>surname</surname>         </travelerdetails>         <infant>             <firstname>infant one</firstname>             <surname>surname</surname>             <dateofbirth>2016-01-06</dateofbirth>         </infant>     </traveler>     <traveler>         <elementnumber>2</elementnumber>         <travelertypecode>adt</travelertypecode>         <travelerdetails>             <firstname>adult two</firstname>             <surname>surname</surname>         </travelerdetails>         <infant>             <firstname>infant two</firstname>             <surname>surname</surname>             <dateofbirth>2017-01-06</dateofbirth>         </infant>     </traveler>     <traveler>         <elementnumber>5</elementnumber>         <travelertypecode>adt</travelertypecode>         <travelerdetails>             <firstname>adult three</firstname>             <surname>surname</surname>         </travelerdetails>     </traveler>     <traveler>         <elementnumber>6</elementnumber>         <travelertypecode>chd</travelertypecode>         <travelerdetails>             <firstname>child one</firstname>             <surname>surname</surname>             <dateofbirth>2013-01-06</dateofbirth>         </travelerdetails>     </traveler> </travelergroup> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -