Search code examples
asp.netxmlasp.net-mvc-4xsltapache-fop

How to show all XSL elements?


The XML file contains Accounts and a list of Account (contains ID and AccountDescription). In the below example, there are 2 Account.

<?xml version="1.0"?>
<Accounts>
 <Account>
  <ID>5</ID>
  <AccountDescription>Account Description 5</AccountDescription>
 </Account>
 <Account>
  <ID>8</ID>
  <AccountDescription>Account Description 8</AccountDescription>
  </Account>
</Accounts>

When using the below XSL, it creates a PDF file with 2 pages, and each page has the header ID and AccountDescription, but there is no data/content underneath it, like this:

On page 1:

ID AccountDescription

On page 2:

ID AccountDescription

I would like to show the data like this:

ID AccountDescription

5 Account Description 5

8 Account Description 8

How can I do that ? Thank you.

This is my current XSL:

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

<xsl:template match="Accounts">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">  
  <fo:layout-master-set>
          <fo:simple-page-master 
              master-name="main"
              margin-top="0px"
              margin-bottom="0px"
              margin-left="18px"
              margin-right="18px">
              <fo:region-body margin-top="0.75in" margin-bottom="2in" margin-left="18px" margin-right="18px"/>
              <fo:region-before extent="0.75in"/>
              <fo:region-after extent="1.5in"/>
              <fo:region-end extent="75px"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <xsl:apply-templates select="Account"/>
</fo:root>
</xsl:template>

<xsl:template match="Account">
<fo:page-sequence master-reference="main">
    <fo:flow flow-name="xsl-region-body">
        <fo:table font-size="10pt">
            <fo:table-column column-width="15mm"/>
            <fo:table-column column-width="55mm"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell >
                        <fo:block text-align="right"><xsl:value-of select="ID"/></fo:block>
                    </fo:table-cell>
                    <fo:table-cell >
                        <fo:block text-align="right"><xsl:value-of select="AccountDescription"/></fo:block>
                    </fo:table-cell>
                    </fo:table-row>
            </fo:table-body>
        </fo:table>
    </fo:flow>

</fo:page-sequence>
</xsl:template>

</xsl:stylesheet>

Solution

  • How can I show all data in the same page ?

    You need to use only one fo:page-sequence. Move it from the Account template up into the Accounts template.

    Updated XSLT

    <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">
    
      <xsl:template match="Accounts">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">  
          <fo:layout-master-set>
            <fo:simple-page-master 
              master-name="main"
              margin-top="0px"
              margin-bottom="0px"
              margin-left="18px"
              margin-right="18px">
              <fo:region-body margin-top="0.75in" margin-bottom="2in" margin-left="18px" margin-right="18px"/>
              <fo:region-before extent="0.75in"/>
              <fo:region-after extent="1.5in"/>
              <fo:region-end extent="75px"/>
            </fo:simple-page-master>
          </fo:layout-master-set>
          <fo:page-sequence master-reference="main">
            <fo:flow flow-name="xsl-region-body">
              <xsl:apply-templates select="Account"/>
            </fo:flow>
          </fo:page-sequence>
        </fo:root>
      </xsl:template>
    
      <xsl:template match="Account">
        <fo:table font-size="10pt">
          <fo:table-column column-width="15mm"/>
          <fo:table-column column-width="55mm"/>
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell >
                <fo:block text-align="right"><xsl:value-of select="ID"/></fo:block>
              </fo:table-cell>
              <fo:table-cell >
                <fo:block text-align="right"><xsl:value-of select="AccountDescription"/></fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </xsl:template>
    
    </xsl:stylesheet>