0

I have the following XML Data:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://kolowrat.at/Letter">
  <letter>
    <sender>
      <title>Mag</title>
      <salutation>Herr</salutation>
      <firstname>Alexander</firstname>
      <lastname>Müller</lastname>
      <street1>Seitenstettengasse 6</street1>
      <zip>3430</zip>
      <city>Linz</city>
      <country>Österreich</country>
    </sender>
    <recipient>
      <title />
      <salutation>Frau</salutation>
      <firstname>Claudia</firstname>
      <lastname>Maier</lastname>
      <street1>Hauptstraße 1</street1>
      <zip>3430</zip>
      <city>Tulln</city>
      <country>Österreich</country>
    </recipient>
    <subject>Ihre Bestellung Nr. 251685489</subject>
    <message>
      <paragraph xsi:type="xsd:string">Ihre Bestellung ist bei uns eingegangen und wird von einem Mitarbeiter vorbereitet und anschließend versandt.</paragraph>
    </message>
  </letter>
  <letter>
    <sender>
      <title>Mag</title>
      <salutation>Herr</salutation>
      <firstname>Michael</firstname>
      <lastname>Haas</lastname>
      <street1>Wienerstraße 1</street1>
      <zip>3430</zip>
      <city>St. Pölten</city>
      <country>Österreich</country>
    </sender>
    <recipient>
      <title />
      <salutation>Herr</salutation>
      <firstname>Claudia</firstname>
      <lastname>Etlinger</lastname>
      <street1>Hauptstraße 1</street1>
      <zip>3430</zip>
      <city>St. Pölten</city>
      <country>Österreich</country>
    </recipient>
    <subject>Ihre Bestellung Nr. 251685489</subject>
    <message>
      <paragraph xsi:type="xsd:string">Ihre Bestellung ist bei uns eingegangen und wird von einem Mitarbeiter vorbereitet und anschließend versandt.</paragraph>
    </message>
  </letter>
</document>

And I want to generate a document with XSL-FO where each letter tag hast got his own page. Here is my XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl fo"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:l="http://kolowrat.at/Letter"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <!-- Set the master / page layout -->
      <fo:layout-master-set>
        <fo:simple-page-master master-name="letter" page-height="29.7cm" page-width="21cm" margin-left="2.5cm" margin-right="2.5cm">
          <fo:region-body margin-top="1.5cm" />
        </fo:simple-page-master>
      </fo:layout-master-set>


      <xsl:for-each select="//letter">
        <fo:page-sequence master-reference="letter">
          <fo:flow flow-name="xsl-region-body">
            <xsl:apply-templates select="sender" />
            <xsl:apply-templates select="recipient"/>
            <xsl:apply-templates select="subject" />
            <xsl:apply-templates select="message" />
          </fo:flow>
        </fo:page-sequence>
      </xsl:for-each>
    </fo:root>

  </xsl:template>

  <!-- sender -->
  <xsl:template match="sender">
    <fo:block text-align="right" margin-left="0cm" padding-left="7cm" border-bottom-style="solid">
      <fo:block>
        <xsl:value-of select="salutation"/>&#x00A0;<xsl:value-of select="title"/>&#x00A0;<xsl:value-of select="firstname"/>&#x00A0;<xsl:value-of select="lastname"/>
      </fo:block>
      <fo:block>
        <xsl:value-of select="street1"/>
      </fo:block>
      <fo:block>
        <xsl:if test="street2 != ''">
          <xsl:value-of select="street2"/>
          <fo:block />
        </xsl:if>
      </fo:block>
      <fo:block>
        <xsl:value-of select="zip"/>&#x00A0;<xsl:value-of select="city"/>
      </fo:block>
      <fo:block>
        <xsl:if test="country != ''">
          <xsl:value-of select="country"/>
          <fo:block />
        </xsl:if>
      </fo:block>
    </fo:block>
  </xsl:template>


  <!-- recipient -->
  <xsl:template match="recipient">
    <fo:block padding-top="1.5cm" padding-bottom="2cm">
      <fo:block>
        <xsl:value-of select="salutation"/>&#x00A0;<xsl:value-of select="title"/>&#x00A0;<xsl:value-of select="firstname"/>&#x00A0;<xsl:value-of select="lastname"/>
      </fo:block>
      <fo:block>
        <xsl:value-of select="street1"/>
      </fo:block>
      <fo:block>
        <xsl:if test="street2 != ''">
          <xsl:value-of select="street2"/>
          <fo:block />
        </xsl:if>
      </fo:block>
      <fo:block>
        <xsl:value-of select="zip"/>&#x00A0;<xsl:value-of select="city"/>
      </fo:block>
      <fo:block>
        <xsl:if test="country != ''">
          <xsl:value-of select="country"/>
          <fo:block />
        </xsl:if>
      </fo:block>
    </fo:block>
  </xsl:template>

  <xsl:template match="subject">
    <fo:block padding-bottom="1.5cm" font-weight="bold">
      <xsl:value-of select="current()"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="message">
    <xsl:for-each select="paragraph">
      <fo:block padding-bottom="0.5cm">
        <xsl:value-of select="current()"/>
      </fo:block>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

When i render this with the FO.NET Driver I get a emtpy pdf. So i guess the tags are not found by my XPath statements. Can anybody help?

Matthias
  • 3
  • 1

1 Answers1

0

This happens due to default namespace of "document" element - xmlns="http://kolowrat.at/Letter"

I'd suggest to do following (specify 'letter' namespace for all descendants of document element in xpath):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl fo"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:l="http://kolowrat.at/Letter"
    xmlns:letter="http://kolowrat.at/Letter"
>

....

      <xsl:for-each select="//letter:letter">
        <fo:page-sequence master-reference="letter">
          <fo:flow flow-name="xsl-region-body">
            <xsl:apply-templates select="letter:sender" />
            <xsl:apply-templates select="letter:recipient"/>
            <xsl:apply-templates select="letter:subject" />
            <xsl:apply-templates select="letter:message" />
          </fo:flow>
        </fo:page-sequence>
      </xsl:for-each>
   ...

  <!-- sender -->
  <xsl:template match="letter:sender">
  ....
</xsl:stylesheet>

See more detailed explanation here: XSLT with XML source that has a default namespace set to xmlns

Community
  • 1
  • 1
Rudolf Yurgenson
  • 603
  • 6
  • 12