Alina B.
Alina B.

Reputation: 1276

xsl to pdf - empty pages

I am experiencing with xsl and pdf output. I have a xml file like this:

<?xml version="1.0" encoding="utf-8" ?>
  <document>
    <header>
      <author>my name</author>
    </header>
    <body>
      <text>some text</text>
    </body>
  </document>

My xsl file looks like:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="/">
  <fo:root>

    <fo:layout-master-set>
      <fo:simple-page-master
        master-name="_title"
        page-height="29.7cm"
        page-width="21cm"
        margin="3cm">
        <fo:region-body/>
      </fo:simple-page-master>

      <fo:simple-page-master
        master-name="_body"
        page-height="29.7cm"
        page-width="21cm"
        margin="3cm">
        <fo:region-body/>
      </fo:simple-page-master>

      <fo:page-sequence-master master-name="titlepage">
        <fo:single-page-master-reference master-reference="_title"/>
      </fo:page-sequence-master>

      <fo:page-sequence-master master-name="body">
        <fo:repeatable-page-master-reference master-reference="_body"/>
      </fo:page-sequence-master>

    </fo:layout-master-set>

    <fo:page-sequence master-reference="titlepage">
      <fo:flow flow-name="xsl-region-body" font-family="Courier" font-size="12pt">
        <fo:block>
          <xsl:apply-templates select="header"/>
        </fo:block>
      </fo:flow>
    </fo:page-sequence>

    <fo:page-sequence master-reference="body">
      <fo:flow flow-name="xsl-region-body" font-family="Courier" font-size="12pt">
        <fo:block>
          <xsl:apply-templates select="body"/>
        </fo:block>
      </fo:flow>
    </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="header">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="body">
    <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>

I expect two pdf pages, first page with "my name" and a second one with "some text". Instead I get two empty pages.

Upvotes: 0

Views: 1399

Answers (1)

Tim C
Tim C

Reputation: 70648

Looking at your XSLT, you have the following template

<xsl:template match="/">

This matches the "document node" of the XML document. This is not the same as the top-level element of the XML document, which is document in your case. / is effectively the parent of the top-level element.

What this means is that later on in your template, when you do the following apply-templates, like so

<xsl:apply-templates select="header"/>
<xsl:apply-templates select="body"/>

These are effectively looking for top-level elements of header and body respectively. However, your header and body elements are child elements of your top-level document element.

To solve this, you have a couple of choices. You could either replace your apply-templates with these:

<xsl:apply-templates select="document/header"/>
<xsl:apply-templates select="document/body"/>

Alternatively, you can change your initial template to match the following (instead of just <xsl:template match="/">)

<xsl:template match="/document">

It is also worth noting, as Dimitre mentioned in his comment, in your templates for header and body, you are doing <xsl:apply-templates />. However, you have no templates matching any child elements of header or body. This means the default templates will kick-in, and just output the text of the elements instead.

Upvotes: 1

Related Questions