简体   繁体   中英

How do I get my xsl-fo table to display like a table?

I am trying out an xsl-fo example that is supposed to make a table, but when I run it it shows the data side by side without table rows or cells. How do I make it display correctly?

I have a stylesheet tag in the .xml file so when I right click it and choose Open with Internet explorer, it should translate it with the .xslt file.

Expected output the two outputs will be in seperate rows,
FO First Office
FO Second Office

My Code

personnel_roster2.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"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match='/rows'>
        <fo:root>

          <fo:layout-master-set>
            <fo:simple-page-master master-name="hello"
                        page-height="11in"  page-width="8.5in" margin-top="1in" 
                        margin-bottom="1in" margin-left="0in" margin-right="1in">
              <fo:region-body margin-top="1in" margin-bottom=".5in"/>
            </fo:simple-page-master>
          </fo:layout-master-set>

            <fo:page-sequence master-reference="hello">
                <fo:flow flow-name="xsl-region-body">
                    <fo:table>
                        <xsl:for-each select="row">
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block font-weight="bold"><xsl:value-of select="@Office"/></fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block font-weight="bold"><xsl:value-of select="@OfficeTitle"/></fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </xsl:for-each>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

personnel_roster2.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "personnel_roster2.xslt"?> 
<rows appMode="DEV" dateGenerated="">
    <row OfficeID="32" OfficeOutputOrder="1" Office="FO" OfficeTitle="First Office" RoomFaxID="88" RoomFaxNumber="" RoomNumberID="123">
        <personnel>
            <person PersonnelID="3870" Name="The Boss" PositionRoomNumberID="31" phone="555-647-5770" PAETitle="" PositionEmployeeTypeID="C" PositionOutputOrder="1" />
        </personnel>
        <officeFaxes>
             <fax RoomNumberID="123" RoomFaxNumber="" OfficeID="32" />
        </officeFaxes>
    </row>
    <row OfficeID="33" OfficeOutputOrder="1" Office="FO" OfficeTitle="Second Office" RoomFaxID="88" RoomFaxNumber="" RoomNumberID="123">
        <personnel>
            <person PersonnelID="3870" Name="The Boss" PositionRoomNumberID="31" phone="555-647-5770" PAETitle="" PositionEmployeeTypeID="C" PositionOutputOrder="1" />
        </personnel>
        <officeFaxes>
             <fax RoomNumberID="123" RoomFaxNumber="" OfficeID="33" />
        </officeFaxes>
    </row>
</rows>

I don't know of a modern browser that natively supports XSL-FO.

So either you directly transform to HTML or transform your XSL-FO output to HTML in a second step with a different XSLT.


An example of the second approach, transforming the output again, is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/fo:root/fo:page-sequence/fo:flow">
        <html>
            <body>
                <xsl:for-each select="fo:table">
                    <table border="1">
                        <xsl:for-each select="fo:table-row">
                            <tr>
                                <xsl:for-each select="fo:table-cell">
                                    <td style="font-weight:{fo:block/@font-weight};">
                                        <xsl:value-of select="fo:block" />
                                    </td>
                                </xsl:for-each>
                            </tr>
                        </xsl:for-each>
                    </table>
                </xsl:for-each>
            </body>
        </html>
     </xsl:template>    

</xsl:stylesheet>

This transforms the output of the first XSLT to a HTML file which can be displayed in the browser.

You do not state anything about an XSL FO processor. XSL FO is a markup language that an application interprets and creates an output. This is the same as HTML ... for HTML the browser interprets the HTML tags and CSS styles and give you a "composed" view in the browser.

To do the same with XSL FO, you use some XSL FO processor which interprets the XSL FO markup and creates output (could be PDF or Postscript or many other composed outputs like AFP, PCL, XPS, ...)

To get true formatted output from XSL FO you need an XSL FO processor. No browser does this. THere is Apache FOP in the open source world or commercial applications like RenderX XEP or AntennaHouse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM