Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Converting html table to xml

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 5/3/2007 4:02:00 PM

craigg75@g... wrote:
> If I have an html table such as:
> 
> <table>
>   <tr class="parent">
>     <td>pdata</td>
>   </tr>
>   <tr class="child">
>     <td>cdataa</td>
>   </tr>
>   <tr class="child">
>     <td>cdatab</td>
>   </tr>
>   <tr class="child">
>     <td>cdatac</td>
>   </tr>
>   <tr class="parent">
>     <td>pdatab</td>
>   </tr>
>   <tr class="child">
>     <td>cdatae</td>
>   </tr>
> </table>
> 
> I'd like to get it to be
> 
> <root>
>   <parent>pdata
>     <child>cdataa</child>
>      <child>cdatab</child>
>     <child>cdatac</child>
>   </parent>
>   <parent>pdatab
>     <child>cdatae</child>
>   </parent>
> </root>

Use a key to group the elements e.g.

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:key name="parent-tr" match="tr[@class = 'child']" 
use="generate-id(preceding-sibling::tr[@class = 'parent'][1])"/>

<xsl:template match="table">
   <root>
     <xsl:apply-templates select="tr[@class = 'parent']" mode="group"/>
   </root>
</xsl:template>

<xsl:template match="tr" mode="group">
   <parent>
     <data><xsl:value-of select="td"/></data>
     <xsl:apply-templates select="key('parent-tr', generate-id(.))"/>
   </parent>
</xsl:template>

<xsl:template match="tr">
   <child><xsl:value-of select="td"/></child>
</xsl:template>

</xsl:stylesheet>

In that solution I have wrapped the parent text content into a data 
element to avoid mixed contents, if you insist on mixed contents then 
take out the <data> above, the grouping solution with the key remains 
unchanged.
-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent