xslt - How can i format XML to desired XML -


i have xml below. tried xsl code transform desired output. not successful. grouping , selecting specific nodes hard tag 'a''s values constant.they can 'math'and 'phys' (except class name). have group these values class name.

input:

<sheet>    <row>       <a>class:x</a>     </row>     <row>       <a>math</a>       <e>10</e>     </row>     <row>       <a>phys</a>       <e>14</e>     </row>       <row>       <a>class:y</a>     </row>       <row>       <a>math</a>        <e>8</e>       </row>       <row>        <a>phys</a>        <e>12</e>       </row>     </sheet> 

desired output:

 <sheet>     <row>       <class_type>x</class_type>       <math>10>/math>        <phys>14>/phys>      </row>      <row>       <class_type>x</class_type>       <math>8>/math>        <phys>12>/phys>      </row> </sheet> 

my attempt:

<xsl:template match="/">     <xsl:apply-templates select="/s0:root"/> </xsl:template> <xsl:template match="/s0:root">     <ns0:sheet>                 <xsl:for-each select="s0:row">                     <xsl:if test="contains(s0:a/text(),'class:')">                         <ns0:class_type>                             <xsl:value-of select="substring-after(s0:a/text(),'class:')"/>                         </ns0:class_type>                     </xsl:if>                     <xsl:if test="contains(s0:a/text(),'math')">                         <ns0:math>                             <xsl:value-of select="s0:e/text()"/>                         </ns0:math>                     </xsl:if>                     <xsl:if test="contains(s0:a/text(),'phys')">                         <ns0:phys>                             <xsl:value-of select="s0:e/text()"/>                         </ns0:phys>                     </xsl:if>                 </xsl:for-each>     </ns0:sheet> </xsl:template> 

here's 1 way @ this:

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:template match="/sheet">     <xsl:copy>         <xsl:for-each select="row[contains(a, 'class:')]">             <row>                 <class_type>                     <xsl:value-of select="substring-after(a, 'class:')"/>                 </class_type>                 <math>                     <xsl:value-of select="following-sibling::row[a='math'][1]/e"/>                 </math>                  <phys>                     <xsl:value-of select="following-sibling::row[a='phys'][1]/e"/>                 </phys>              </row>         </xsl:for-each>     </xsl:copy> </xsl:template>  </xsl:stylesheet> 

this assuming every "class" row has "math" , "physics" rows following before next "class" row comes along.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -