473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Flat HTML headers to nested XML sections

I am working on creating an XSLT that transforms Html into an XML
format that can be imported into Framemaker. The challenge, it turns
out, is correctly transforming the flat html header tags (<H1>, <H2>,
etc)
into nested sections inside the xml. I have made significant
progress, but have run into a roadblock.

Here is an example of my input HTML:

<html><body>
<p>abc abc</p>
<h1 class='header'>A</h1>
<p>A abc abc</p>
<h2 class='header'>B</h2>
<p>B abc abc</p>
<h3 class='header'>C</h3>
<p>Cabc abc</p>
<h2 class='header'>D</h2<!-- this is missing in the output --
>
<p>D abc abc</p<!-- this is missing in the output -->
<h1 class='header'>E</h1>
<p>E abc abc</p>
</body></html>

Here is an example of the output, you'll notice that the <H2>D</h2>
is missing.

<?xml version="1.0" encoding="UTF-8"?>
<article>
<title/>
<para>abc abc</para>
<section depth="1" id="A">
<title>A</title>
<para>A abc abc</para>
<section depth="2" id="B">
<title>B</title>
<para>B abc abc</para>
<section depth="3" id="C">
<title>C</title>
<para>C abc abc</para>
</section>
</section>
</section>
<section depth="1" id="E">
<title>E</title>
<para>E abc abc</para>
</section>

The problem is that my code is currently applying templates to all
nodes following a header who's nearest preceding header is that same
header. For this reason when content follows a header which isn't
it's header (like an <h2following an <h3>) it doesn't get shown.
What I don't understand is how to fix it. Any help would much
appreciated. I'm not really an xsl guru, so I'm doing the best I can
to get through this.

Here is the relevant code from my xsl:

<xsl:template match="body">
<article>
<title>
<xsl:value-of select="$docTitle" />
</title>

<xsl:for-each select='child::*[not(preceding-
sibling::*[@class="header"])][not(@class="header")]'>
<xsl:apply-templates select="."/>
</xsl:for-each>

<xsl:variable name='depth'
select='substring(name(child::*[@class="header"][1]),2)'/>
<xsl:for-each select='child::*[@class="header"]
[substring(name(),
2)&lt;=$depth]'>
<xsl:apply-templates select="."/>
</xsl:for-each>

</article>
</xsl:template>

<xsl:template match="h1 | h2 | h3 | h4 | h5">
<xsl:call-template name="header">
<xsl:with-param name="depth" select="substring(name(),2)"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="header">
<xsl:param name="depth"/>
<section>
<xsl:attribute name="depth">
<xsl:value-of select="$depth"/>
</xsl:attribute>

<xsl:attribute name="id">
<xsl:value-of select="translate(.,' ','')" />
</xsl:attribute>
<title><xsl:value-of select="."/></title>

<xsl:variable name='thisHeader' select='generate-id(.)'/>
<xsl:for-each select='following-sibling::*[$thisHeader=generate-
id(preceding-sibling::*[@class="header"][last()])]
[not(@class="header") or (@class="header" and substring(name(),2)>=
$depth)]'>
<xsl:apply-templates select="."/>
</xsl:for-each>

</section>

</xsl:template>

May 16 '07 #1
3 2610
CrazyAtlantaGuy wrote:
I am working on creating an XSLT that transforms Html into an XML
format that can be imported into Framemaker. The challenge, it turns
out, is correctly transforming the flat html header tags (<H1>, <H2>,
etc) into nested sections inside the xml.
This is called encapsulation, and there's a much neater way than writing
XSLT to try and reach-forward-down-the-tree-up-to-but-not-including the
next H1/H2/H3/etc.

1. Run Tidy to make the HTML into well-formed XHTML (tidy -nc -asxml)

2. Write a short script to turn the XHTML back into valid SGML
(remove NETs, namespaces)

3. Apply a DocType Declaration for the ISO 15445 HTML DTD, which
includes a DIV1/DIV2 containment structure, in "preparation" mode
(declare % Preparation as INCLUDE in the internal subset and use
pre-html as the declared root element type)

4. Run osgmlnorm to normalize the document: this adds the missing
markup, switches single quotes to double where possible, etc

<!doctype pre-html
public "ISO/IEC 15445:2000//DTD HyperText Markup Language//EN" [
<!entity % Preparation "include" >
]>
<PRE-HTML>
<HEAD>
<META CONTENT="HTML Tidy for Linux/x86 (vers 1 September 2005), see
www.w3.org" NAME="GENERATOR">
<TITLE></TITLE>
</HEAD>
<BODY>
<P>abc abc</P>
<H1 CLASS="header">A</H1>
<DIV1>
<P>A abc abc</P>
<H2 CLASS="header">B</H2>
<DIV2>
<P>B abc abc</P>
<H3 CLASS="header">C</H3>
<DIV3>
<P>Cabc abc</P>
</DIV3>
</DIV2>
<H2 CLASS="header">D</H2>
<DIV2>
<P>D abc abc</P>
</DIV2>
</DIV1>
<H1 CLASS="header">E</H1>
<DIV1>
<P>E abc abc</P>
</DIV1>
</BODY>
</PRE-HTML>

You can easily mess with the Preparation structure in the DTD if you
don't like the way they did it (I don't).

///Peter
May 16 '07 #2
You could try adapting something from the XSLT FAQ. Likely candidates
would be
http://www.dpawson.co.uk/xsl/sect2/N4486.html#d5891e424
or
http://www.dpawson.co.uk/xsl/sect2/N...tml#d5891e1051

Some of the other examples on that page may also be adaptable to this
question.

(It's always worth checking Dave's page; he has done an excellent job of
collecting useful answers from XSL-List, which is unofficial but has
been in existence since before XSL was a Recommendation and has had
participation by a lot of XSL's architects and implementers. I still try
to keep half an eye on that list, though I must admit I don't watch it
as closely as I should.)

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
May 17 '07 #3
On May 17, 12:37 am, Joe Kesselman <keshlam-nos...@comcast.netwrote:
You could try adapting something from the XSLT FAQ. Likely candidates
would behttp://www.dpawson.co.uk/xsl/sect2/N4486.html#d5891e424
orhttp://www.dpawson.co.uk/xsl/sect2/N4486.html#d5891e1051

Some of the other examples on that page may also be adaptable to this
question.

(It's always worth checking Dave's page; he has done an excellent job of
collecting useful answers from XSL-List, which is unofficial but has
been in existence since before XSL was a Recommendation and has had
participation by a lot of XSL's architects and implementers. I still try
to keep half an eye on that list, though I must admit I don't watch it
as closely as I should.)

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Thanks for the help!

May 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: .d.hos | last post by:
ok, fairly new to python, relatively familiar w/ ms-sql. here's my issue: my .py script parses the contents of a (tab delim.) flat file, then attempts to insert the information into the db. I've...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
4
by: charles | last post by:
Using XSL, how can I go from this format: <T4> <T4_slip>1</T4_slip> <T4_slip>2</T4_slip> <T4_slip>3</T4_slip> <T4_slip>4</T4_slip> <T4_summary>20</T4_summary> <T4_slip>1</T4_slip>...
1
by: tom | last post by:
hello, i have seen multiple postings on the subject but no answer that addresses my question: I create a dataset using a xsd schema. the schema specifies a relation from one of the tables to...
1
by: Anders Nilsson | last post by:
I'd like to know if there is support in .NET to somehow "flatten" a nested XML schema. Here is the situation: Currently I have code that can validate nested XML against a schema. The XML is...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
2
by: Sue | last post by:
Nested datagrid within outer datagrid. Both datagrids use tables to format data in both the itemtemplate and edittemplate sections. Having problems with the format of once of the tablecells in the...
17
by: Grant Kelly | last post by:
I'm wondering if it's possible within HTML markup (or possibly CSS) to specify that an HTML table's headers should be placed 'over' the cell borders rather than 'within' the cells. For example...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.