[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
</xsl:template>
</xsl:stylesheet>
On 6/5/06, Duncan Anker <danker@xxxxxxxxxxxxx> wrote:
Re: [xsl] Matching sequential elements and suppression of nodes
Subject: Re: [xsl] Matching sequential elements and suppression of nodes From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx> Date: Mon, 5 Jun 2006 18:03:56 +0530 |
Hi Duncan, This looks like a positional grouping problem. The following stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/html"> <x> <xsl:apply-templates select="br" /> </x> </xsl:template>
<xsl:template match="br[normalize-space(preceding-sibling::node()[1]) = ''][local-name(preceding-sibling::node()[2]) = 'br'][1]"> <dl> <xsl:call-template name="dummy"> <xsl:with-param name="nodeset" select="following-sibling::node()" /> </xsl:call-template> </dl> </xsl:template>
<xsl:template name="dummy"> <xsl:param name="nodeset" />
<xsl:for-each select="$nodeset"> <xsl:choose> <xsl:when test="self::b"> <dt><xsl:copy-of select="node()" /></dt> </xsl:when> <xsl:when test="self::text()[normalize-space() != '']"> <dd><xsl:copy-of select="." /></dd> </xsl:when> </xsl:choose> </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When applied to XML: <html> <br/> <br/> <b>word:</b> definition<br/> <b>word:</b> definition<br/> <br/> <br/> </html>
produces output: <x> <dl> <dt>word:</dt> <dd> definition</dd> <dt>word:</dt> <dd> definition</dd> </dl> </x>
Regards, Mukul
On 6/5/06, Duncan Anker <danker@xxxxxxxxxxxxx> wrote:
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Matching sequential elements , Duncan Anker | Thread | Re: [xsl] Matching sequential eleme, Wendell Piez |
Re: [xsl] Top 100 rows and sort the, David Carlisle | Date | Re: [xsl] Office 2007, XSL-FO, and , Keith Fahlgren |
Month |