[xsl] Matching sequential elements and suppression of nodes

Subject: [xsl] Matching sequential elements and suppression of nodes
From: Duncan Anker <danker@xxxxxxxxxxxxx>
Date: Mon, 05 Jun 2006 12:42:43 +1000
Hi list,

I am playing around with transforming something like this:

<br />
<br />
<b>word:</b> definition<br />
<b>word:</b> definition<br />
<br />
<br />

into something like this:

<dl>
<dt>word:</dt><dd>definition</dd>
<dt>word:</dt><dd>definition</dd>
</dl>

Thus far, I have a template to match each definition:

 <xsl:template match="b[name(preceding-sibling::*[1]) = 'br']">
 <dt><xsl:value-of select="."  /></dt>
 <dd><xsl:value-of select="following-sibling::text()[1]" /></dd>
 </xsl:template>

It seems to work, in a fashion. It matches <br />foo<b> as well as <br /><b> although I believe I saw a post somewhere in the archives about determining if a text node is whitespace, so if needs be I could test for that instead of being lazy and making assumptions about my data.

What is not so obvious to me is how to stop the following text node from being output twice. I was hoping there was some way to delete a node from the source tree, or mark it as already-processed, or something like that. At the moment the best I can think of is a template that matches text() followed by <br />, although there's no guarantee that that won't be somewhere else in the document where it shouldn't be transformed.

The rule I already have should be restricted to portions of the document as well, since <br /><b> could potentially show up anywhere. I'm thinking that perhaps I should be searching for a <br /><br /> ... <br /><br /> and trying to treat it as a subtree, i.e. wrap it in <dl></dl> and process the contents, then somehow suppress the output of the entire block.

While this type of work is what XSLT is designed for (I assume; maybe it was a prank that was taken seriously), I am unable to see any easy way to handle the case where it is not well-formed XML, rather sequential tags all at the same level. Is it just a matter of writing hideous XPath expressions to traverse up and down the axes?

Regards,
Duncan

Current Thread