Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: HTML Parsing Question

From: Peter Flynn <peter.nosp@-.--------.-->
To: NULL
Date: 1/2/2007 2:09:00 AM

Stefan Kleineikenscheidt wrote:
> Hi all,
> 
> i'm trying to convert an HTML page to a hierachical structure, but I am
> stuck.  Consider a page like that:
> 
> <h1>First Heading1</h1>
>   <p>some text</p>
>   <p>more text</p>
> 
>   <h2>First Heading2</h2>
>     <p>more text</p>
> 
>   <h2>Second Heading2</h2>
>     ...
> <h1>Second Heading1</h1>
>   ...
>   <h2>Third Heading2</h2>
>     ...

First of all you would need to make it well-formed XHTML (use W3C Tidy
for that). This ensures that any subsequent XSLT process won't gag.

> This is my 'h1' template, where i try to process all elements between
> two 'h1' elements:
> 
>   <xsl:template match="//h:h1">
>     <section>
>       <title><xsl:value-of select="text()" /></title>
>       <xsl:variable name="nexth1" select="position(parent::*/*[(name()
> = 'h1')])" />
>       <xsl:apply-templates select="following-sibling::*[position()
> &lt;= $nexth1]" />
>     </section>
>   </xsl:template>

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">

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

   <xsl:template match="h1|h2|h3|h4">
     <xsl:variable name="id" select="generate-id(.)"/>
     <xsl:variable name="level">
       <xsl:value-of select="number(translate(name(),'h',''))"/>
     </xsl:variable>
     <xsl:variable name="gi" select="name()"/>
     <xsl:element name="{concat('sect',$level)}">
       <xsl:attribute name="id" select="$id"/>
       <title>
         <xsl:apply-templates/>
       </title>
       <xsl:apply-templates select="following-sibling::*
           [generate-id(preceding-sibling::*[name()=$gi][1])=$id]
           [not(substring(name(),1,1)='h' and name()!='hr' and
            number(translate(substring(name(),1,1),'h',''))&lt;$level)]
 
[not(number(translate(name(preceding-sibling::*[substring(name(),1,1)='h' 
and name()!='hr'][1]),'h',''))&lt;$level)]"/>
     </xsl:element>
   </xsl:template>

   <xsl:template match="p">
     <para>
       <xsl:apply-templates/>
     </para>
   </xsl:template>

</xsl:stylesheet>

This needs some more work: it's not subsetting out the higher-level H* 
element types, but I've run out of time here.

///Peter
-- 
XML FAQ: http://xml.silmaril.ie/


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