Re: [xsl] n-value of preceding lb-element

Subject: Re: [xsl] n-value of preceding lb-element
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Mon, 13 Aug 2001 14:55:15 -0400
[Ingo Mittendorf]

> What I want to generate from the example text below is this table (no
> more, no less):
>
> LINE ERROR      CORRECTION
> 1 sayed      said
> 2 tearfull   tearful
> 2 four       for
> 5 milions    millions
>

Here's a stylesheet that produces your output from your xml example.  First,
the output:

--------------------------------------------------
LINE ERROR CORRECTION

[line 1]  sayed  said.

[line 2] tearfull  tearful.

[line 2] four  for.

[line 5] milions  millions.
-------------------------------------------------

The hardest part is control of line breaks in text output , which can be
different for different xslt processors.  Some of the slightly strange
formatting of the stylesheet is there to produce the right text formatting
(including the periods (".") at the ends of the lines).  If you wanted to
get HTML tables, they wouldn't be necessary.   If you run this stylesheet
and the output formatting is different, it may be because some lines got
refomatted by your email client.  I've tried to keep them short enough that
this wouldn't happen, but I can't be sure.

Of course, you could change the "[line 2]" format to just "2" - I've left it
this way for readability.

I'm not absolutely sure I have handled all the possible cases, but it should
be close enough for you to evolve if needed.

The key points are

1) Line breaks can be children of either a <p> or an <s> element, so there
is a template with a different mode for each case.

2) For each <lb> within an <s> element, the lb template looks to see if
there is a following sibling <corr> element.  If not, no output is
generated.

3) For each <corr> element, the template checks to see if the immediately
preceeding sibling was an <lb>.  If not, it finds the line break number of
the first preceeding <lb> it finds within the same <s> element, and outputs
that for the line number.  Otherwise, there would be no way to output the
line number.

Here's the stylesheet:

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

<xsl:template match="/example">
<results>
LINE ERROR CORRECTION
<xsl:apply-templates select='p'/>
</results>
</xsl:template>

<xsl:template match='p'>
 <xsl:apply-templates select='lb|s' mode='in_p'/>
</xsl:template>

<xsl:template match='s' mode='in_p'>
 <xsl:apply-templates select='lb|corr' mode='in_s'/>
</xsl:template>

<xsl:template match='s' mode='in_s'>
 <xsl:apply-templates select='lb|corr' mode='in_s'/>
</xsl:template>

<xsl:template match='corr' mode='in_s'>
 <xsl:variable name='lastLB'
    select='name(preceding-sibling::*[1])="lb"'/>
 <xsl:variable name='lastn'
    select='preceding-sibling::lb/@n'/>
 <xsl:if test='not($lastLB) and $lastn>0'
    >[line <xsl:value-of select='$lastn'/>]</xsl:if
 >&#32;<xsl:value-of select='@sic'/> &#32;<xsl:value-of select='.'/>.
</xsl:template>

<xsl:template match='lb' mode='in_p'
 ><xsl:variable name='n' select='@n'
 />[line <xsl:value-of select='$n'/>] <xsl:apply-templates
     select='corr' mode='in_s'/>
</xsl:template>

<xsl:template match='lb' mode='in_s'
 ><xsl:if test='name(following-sibling::*)="corr"'
  >[line <xsl:value-of select='@n'/>]</xsl:if></xsl:template>

</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread