xsl-list
[Top] [All Lists]

Re: Exclude elements in transformation

2004-09-17 05:17:04
Hi Sönke,

So you want to

- in the template for heading elements, process the heading *and* its next 
sibling
  (inside a table, so they can be kept together)
- then, process the following siblings *except* those that immediately follow
  a heading, because they have already been processed ?

1. The second element is there twice.

There's a few things in your template that I don't understand:

<xsl:template match="h1|h2|h3|h4|h5">
    <table>
        <row keep-with-next="always">
            <xsl:apply-templates/>
        </row>
        <row>
            <xsl:apply-templates select="following-sibling::*"/>
        </row>
    </table>
</xsl:template>

The second apply-templates will select *all* the following sibling
elements, maybe you meant "following-sibling::*[1]" here?

Then:
<xsl:template match="*[preceding-sibling::(h1|h2|h3|h4|h5)]"/>

My processor doesn't accept this: an axis name cannot be followed by '('
You could do "*[preceding-sibling::*[1][starts-with(local-name(), 'h')]]"

I have no knowledge of XSL-FO, but here's a small xml2xml test stylesheet
that might be of help:

--- test.xsl ---
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="/*">
  <result>
   <xsl:apply-templates/>
  </result>
 </xsl:template>
 <xsl:template match="h1|h2|h3|h4|h5">
  <table>
   <title><xsl:value-of select="."/></title>
   <xsl:variable name="next" select="following-sibling::*[1]"/>
   <xsl:if test="$next and not(starts-with(local-name($next), 'h'))">
    <line1><xsl:value-of select="$next"/></line1>
   </xsl:if>
  </table>
 </xsl:template>
 <xsl:template match="*">
  <!-- skip first line -->
  <xsl:if test="not(preceding-sibling::*[1][starts-with(local-name(), 'h')])">
   <line><xsl:value-of select="."/></line>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

--- input.xml ---
<div>
   <h1>One</h1>
   <p>some</p>
   <p>text</p>
   <p>paragraphs</p>
   <h1>Two</h1>
   <p>more</p>
   <p>text</p>
   <h2>Three</h2>
   <h2>Four</h2>
   <p>still</p>
   <p>more</p>
   <p>text</p>
   <h4>Five</h4>
</div>

--- output.xml ---
<result>
   <table>
      <title>One</title>
      <line1>some</line1>
   </table>
   <line>text</line>
   <line>paragraphs</line>
   <table>
      <title>Two</title>
      <line1>more</line1>
   </table>
   <line>text</line>
   <table>
      <title>Three</title>
   </table>
   <table>
      <title>Four</title>
      <line1>still</line1>
   </table>
   <line>more</line>
   <line>text</line>
   <table>
      <title>Five</title>
   </table>
</result>

Maybe there are better ways, much depends on the exact structure of your
input files, and how you want to output it. Eg. you could turn it around and
omit the headings instead of the first paragraphs:

 <xsl:template match="h1|h2|h3|h4|h5"/>
 <xsl:template match="*">
  <xsl:variable name="prev" select="preceding-sibling::*[1]"/>
  <xsl:choose>
   <xsl:when test="$prev[starts-with(local-name(), 'h')]">
    <table>
     <title><xsl:value-of select="$prev"/></title>
     <line1><xsl:value-of select="."/></line1>
    </table>
   </xsl:when>
   <xsl:otherwise>
    <line><xsl:value-of select="."/></line>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

Also note that the [starts-with(local-name(), 'h')] test assumes that there are
no other elements starting with 'h' (apart from h1|h2|h3|h4|h5). If that's a
problem you could do something like

    [contains(';h1;h2;h3;h4;h5;', concat(';', local-name(), ';'))]

2. The xsl:use-attribute-sets="name()" does not work here.

I don't know, what if you try "local-name()" instead?

Best regards,
Anton Triest


Sönke Ruempler wrote:

I have a working XSLT Stylesheet for transforming a html-like syntax into
XSL-FO.

The output PDF should have the feature that a Heading never breaks with the
next elements into 2 pages.

XSL-FO has the the property "keep-with-next" for this issue, but I use FOP
as FO-processor and that only supports it within 2 table rows. So i needed a
template, that converts the <hX> tag and the following tag into a <fo:table>
with 2 rows. And the second template, that tells XSLT not to process the
elements preceeded by a <hX> tag twice. And i wrote you my stylesheets, but
the dont work:


[XSLT]
<xsl:template match="h1|h2|h3|h4|h5">
<fo:table table-layout="fixed" width="100%">
<fo:table-column
column-width="proportional-column-width(1)"/>
<fo:table-body>
<fo:table-row keep-with-next="always">
<fo:table-cell>
<fo:block
xsl:use-attribute-sets="name()">
<xsl:apply-templates/>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>
<xsl:apply-templates
select="following-sibling::*"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>

<xsl:template match="*[preceding-sibling::(h1|h2|h3|h4|h5)]"/>
[/XSLT]

But that does not work

1. The second element is there twice.
2. The xsl:use-attribute-sets="name()" does not work here.

Any idea?

So it's just a workaround and would not be possible if FOP supported the
"keep" for all elements as the standard says. I am happy with my 'dirty'
solution, because if FOP will support it the future, the workaround gets
kicked.

I would be really happy if you told me how to get this work!!

Thx, soenke.



<Prev in Thread] Current Thread [Next in Thread>