Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Extending headings with XML

Rated 3.7 (Ratings: 10) (Add your rating)

Log in to add a comment
(8 comments so far)

Want more?

 
Picture of luminosity

Lachlan Cannon

Member info | Full bio

User since: October 13, 2001

Last login: October 13, 2001

Articles written: 5

Headings as they stand today

The headings <h1> through <h6> are very basic ways of representing headings within a document. Although the method stands up well enough to mark up basic documents, it doesn't stand up to everything we can throw at it.

It would have been much nicer if from the start the header tags had been defined something along the lines of the following, from day one:

<h1 text="top heading">
	<h2 text="lower heading">
		<p>random content</p>
	</h2>
	<p>text unassociated with the h2</p>
</h1>

A refinement of headings

Obviously though, this is not the case. This doesn't mean that we're not able to associate headers with their content, however, just that we have to do it ourselves. This is where the extensibility of XML can help you if you're using XHTML.

Because of the namespacing abilities of XML we can mix in our own custom markup with our XHTML. We can use this to produce a fully degradable (HTML browsers are supposed to ignore unknown elements) container element to associate our headings with their content.

First of all we need to declare the namespace for our heading extensions. For the purposes of this exercise let's use the URL http://members.evolt.org/luminosity/xml/header/. Now, to incorporate our extended elements we change our base tag from &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;, to

&lt;html xmlns="http://www.w3.org/1999/xhtml"
xmlns:header="http://members.evolt.org/luminosity/xml/header/" xml:lang="en"&gt;
.

Now whenever we run across a header we can associate it with its content using our custom designed tags. Every time you see a header, put a &lt;header:hn&gt; before the header, and a &lt;/header:hn&gt; after the last of its associated content. For example, if we start off with the following code sample:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<title>The supermarket fiasco</title>
	</head>
	<body>
		<h1>The supermarket fiasco</h1>
		<h2>The beginning</h2>
		<p>In the beginning was the supermarket.
		The supermarket dished out your typical
		bland supermarket food.</p>
		…
		<h2>The end</h2>
		<p>And so the public cheered, as the last
		supermarket burnt, and their taste buds
		were once again put to use.</p>
	</body>
</html>

We replace it with this:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:header="http://members.evolt.org/luminosity/xml/header/" xml:lang="en">
	<head>
		<title>The supermarket fiasco</title>
	</head>
	<body>
		<header:h1>
		<h1>The supermarket fiasco</h1>
		<header:h2>
		<h2>The beginning</h2>
		<p>In the beginning was the supermarket.
		The supermarket dished out your typical
		bland supermarket food.</p>
		…
		</header:h2>
		<header:h2>
		<h2>The end</h2>
		<p>And so the public cheered, as the last
		supermarket burnt, and their taste buds
		were once again put to use.</p>
		</header:h2>
		</header:h1>
	</body>
</html>

The optional extras

So how does this help us? Well it satisifies those of us who wish that header had been a container element for its content all along. You can regard the h1 inside our h1 the same as the legend inside a fieldset tag, if you're not happy with having the same data seemingly defined twice. You can also set the text of the original h1 to be an attribute for your new h1 if you want. Use something like: &lt;header:h1 header:text="The supermarket fiasco"&gt;. In the future, when browser support is good enough you could then eliminate the original hn tags and use the before pseduo-class with generated content to print out the header (header:hn:before {content: attr(header:text);}), although I would recommend against this, because your new element won't have semantic meaning to browsers, whereas the old hn tags do.

We can of course use the new tags to style all content based on their level of heading — in fact this would be the only practical usage that I can imagine at this time. Something along the lines of:

header:h1 {
	background-color: blue;
}

header:h2 {
	background-color: light blue;
}

And of course, you can further extend the new headers with your own attributes, and you could then use them with some nifty JavaScript or what have you.

Also, you're not limited to extending header elements. You can also use your own namespaces to create documents that use your own tags to fill in for other elements that you need, or want, that HTML doesn't provide yet.

Born in Melbourne, Australia, Lachlan is soon to begin study at University.

Lachlan delights in being sent money, and other small gifts. But if you really have the money to spare ;) give it to something more worthy.

why not just &lt;div&gt;?

Submitted by bedwardj on July 5, 2002 - 08:20.

It seems like a lot of extra work when you could just put a &lt;div id="headersubject"&gt;&lt;/div&gt; around the &lt;hn&gt; and following content. Would you still be able to style the actual heading text as a block level element if you rid yourself of &lt;hn&gt; tags in lieu of &lt;header:hn text="heading"&gt;?

However, I think this article does show the value extending xhtml affords developers.

login or register to post comments

RE: why not just &lt;div&gt;?

Submitted by luminosity on July 5, 2002 - 09:19.

But the beauty of the system is that you can add your own custom attributes too, something which you can't do with <div>s.

You could still style the heading text as block level yada yada, whatever you wanted if you had the attribute. Something along the lines of:

header:hn:after {
        content: attr(text);
        display: block;
        color: blue;
        font-size: 16px;
        font-weight: bold;
}

login or register to post comments

RE: why not just &lt;div&gt;?

Submitted by bedwardj on July 5, 2002 - 10:31.

That's quite slick!

Then it is just a semantics game for me. I view a document as having several sections and sub-sections. Each (sub-)section has it's own header and associated content. The header describes the content following, but does not contain it.

I think my interpretation would go something like this:

&lt;section:div1 header="Header One"&gt;Section One content&lt;/section:div1&gt;

section:div1:before {
    content: attr(header);
    display: block;
}

login or register to post comments

They are really sections

Submitted by Martin Tsachev on July 7, 2002 - 19:52.

Headers is not the right word for a container of more than a line, section is the right word. Besides look at DocBook they have it too and it's sect (or something) with a title element for the header of the section.

login or register to post comments

RE: They are really sections

Submitted by luminosity on July 7, 2002 - 22:42.

If I was actually going to use this, I doubt I'd use header as the name of the extension either, preferring to use something like hext (to stand for html extensions), and then I would use that to extend all the elements I wanted to such as adding a &lt;hext:book&gt; element to wrap around book titles, a &lt;hext:game&gt; to wrap around game titles, etc etc.

This article was not intended to point out the absolute correct way you must do it, but rather to show you how you can do something like this yourself. You are quite right however, about them being sections not headers.

login or register to post comments

why not just &lt;div&gt;?

Submitted by freq on July 12, 2002 - 15:48.

Apologies if this is a dumb question but could someone describe why this would be useful? It looks good and it is a good article but in what scenerio could it be used ?

login or register to post comments

RE: why not just &lt;div&gt;

Submitted by luminosity on July 13, 2002 - 00:06.

Mainly, a div has a fixed set of attributes, therfore all you can use it for is to wrap and identify the content (which is still good, of course). If you use your own element from a custom namespace, you can include other attributes that you can use with CSS generated content, or for creating behaviours with JavaScript.

login or register to post comments

Example of using Div in the W3 specs

Submitted by BenM on July 18, 2002 - 15:38.

All in all an interesting example of using xml namespaces. From the W3 specs we can read the following (section 7.5.5 Headings: The H1, H2, H3, H4, H5, H6 elements)

The following example shows how to use the DIV element to associate a heading with the document section that follows it. Doing so allows you to define a style for the section (color the background, set the font, etc.) with style sheets.


Forest elephants

In this section, we discuss the lesser known forest elephants. ...this section continues... Habitat

Forest elephants do not live in trees but among them. ...this subsection continues...

Perhaps titling the article differently would have attracted more interest, this is a good example of how to use XML namespaces with XHTML, unfortunately that is not obvious from the title, my first impression was I already know how to do that (using divs) I nearly didn't come to read it! Glad I did

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.