Link to home
Start Free TrialLog in
Avatar of charles_cp
charles_cp

asked on

Convert xml and xsl to xsl-fo

I wrote an choose.xml and choose.xsl file. It’s run like this:

Lesson ID: 11 Lesson Name: Maths
Student ID      Name      Age      Sex
984610      John      25      M
984612      May      23      F
Lesson ID: 12 Lesson Name: English
Student ID      Name      Age      Sex
984611      Eric      26      M
984613      Rose      25      F


I want to generate a PDF with the same format. So I designed to create another file called choosefo.xsl. However, I don’t know how to do that. Can anyone help me to convert the xml and xsl to xsl-fo? Thank you very much!

-------choose.xml----------
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="choose.xsl"?>
<choose_system>
     <lessons>
          <lesson id="11">
               <name>Maths</name>
          </lesson>
          <lesson id="12">
               <name>English</name>
          </lesson>
     </lessons>
     <students>
          <student id="984610" lesson_id="11">
               <name>John</name>
               <age>25</age>
               <sex>M</sex>
          </student>
          <student id="984611" lesson_id="12">
               <name>Eric</name>
               <age>26</age>
               <sex>M</sex>
          </student>
          <student id="984612" lesson_id="11">
               <name>May</name>
               <age>23</age>
               <sex>F</sex>
          </student>
          <student id="984613" lesson_id="12">
               <name>Rose</name>
               <age>25</age>
               <sex>F</sex>
          </student>
     </students>
</choose_system>


----------choose.xsl-----------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="choose_system/lessons/lesson">
                    Lesson ID: <xsl:value-of select="@id"/>
                    Lesson Name: <xsl:value-of select="name"/> <br></br>
                    <table border="1">
                        <tr bgcolor="yellow">
                            <th align="left">Student ID</th>
                            <th align="left">Name</th>
                            <th align="left">Age</th>
                            <th align="left">Sex</th>
                        </tr>
                        <xsl:for-each select="//choose_system/students/student[@lesson_id = current()/@id]">
                            <tr>
                                <td><xsl:value-of select="@id"/></td>
                                <td><xsl:value-of select="name"/></td>
                                <td><xsl:value-of select="age"/></td>
                                <td><xsl:value-of select="sex"/></td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
charles_cp,

I just saw that my previous post contained the XSL-FO
not the XSLT to get there
here it is

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="only">
                    <fo:region-body region-name="xsl-region-body"
                        margin="1cm" padding="6pt"/>
                    <fo:region-before  region-name="xsl-region-before"
                        extent="1cm"/>
                    <fo:region-after  region-name="xsl-region-after"
                        extent="1cm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="only">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block>
                        <fo:table>
                            <fo:table-body>
                            <fo:table-row background="yellow">
                                <fo:table-cell><fo:block>Student ID</fo:block></fo:table-cell>
                                <fo:table-cell><fo:block>Name</fo:block></fo:table-cell>
                                <fo:table-cell><fo:block>Age</fo:block></fo:table-cell>
                                <fo:table-cell><fo:block>Sex</fo:block></fo:table-cell>
                                <fo:table-cell><fo:block>Lesson ID</fo:block></fo:table-cell>
                            </fo:table-row>
                    <xsl:for-each select="//choose_system/lessons/lesson/students/student">
                        <fo:table-row>
                            <fo:table-cell><fo:block><xsl:value-of select="@sID"/></fo:block></fo:table-cell>
                            <fo:table-cell><fo:block><xsl:value-of select="name"/></fo:block></fo:table-cell>
                            <fo:table-cell><fo:block><xsl:value-of select="age"/></fo:block></fo:table-cell>
                            <fo:table-cell><fo:block><xsl:value-of select="sex"/></fo:block></fo:table-cell>
                            <fo:table-cell><fo:block><xsl:value-of select="cID"/></fo:block></fo:table-cell>
                        </fo:table-row>
                    </xsl:for-each>
                            </fo:table-body>
                        </fo:table>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

here is what you should do
- run the XSLT on the XML file
- save the resulting xsl-FO file
- use a XSL-FO processor to render this XSL-FO in PDF

I use Antenna House or FOP for the latter process
Antenna House can process the XSLT as well, so XML plus XSL returns PDF

cheers