Re: [xsl] converting html table to xml

Subject: Re: [xsl] converting html table to xml
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 29 Nov 2002 16:40:49 +0000
Hi Thomas,

> I have an html table that looks like the one included below. I want
> to convert it to an xml file that looks like the one shown below. I
> haven't had much luck, but I have included my xsl attempt below, but
> it doesn't quite get me what I am looking for.
>
> Here is the sample xml
> <table>
> <tr>
>         <td><a>lateletters</a></td>
>         <td>xx</td>
> </tr>
> <tr>
>         <td>yy</td>
>         <td>zz</td>
> </tr>
> <tr>
>         <td><a>earlyletters</a></td>
>         <td>aa</td>
> </tr>
> <tr>
>         <td>bb</td>
>         <td>cc</td>
> </tr>
> </table>
>
> Here is what I want the output to look like
> <frag>
> <letters name="lateletters">
> <letter>xx</letter>
> <letter>yy</letter>
> <letter>zz</letter>
> </letters>
> <letters name="earlyletters">
> <letter>aa</letter>
> <letter>bb</letter>
> <letter>cc</letter>
> </letters>
> </frag>

Your problem is how to get the <td> elements containing the letters
that belong to one of your letter groups. I think that the easiest way
to do this is to set up a key that indexes each <td> element that
doesn't contain an <a> element by the value of the closest preceding
<a> element, which you can do with:

<xsl:key name="letters" match="td[not(a)]"
         use="preceding::a[1]" />

The XSLT then looks similar to what you have already, except that you
can use the key to get at the letters associated with each letter
group:

  <xsl:for-each select="tr/td/a">
    <letters name="{.}">
      <xsl:for-each select="key('letters', .)">
        <letter><xsl:value-of select="."/></letter>
      </xsl:for-each>
    </letters>
  </xsl:for-each>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread