Solved

Transforming Java Objects to XML and Displaying Using XSL

Posted on 2004-10-08
3
290 Views
Last Modified: 2010-04-17
I have a Java Object that represents column names.  I am using FOP to convert the Object to PDF.  How should my XSL block look to display these column names if I add all the column names to an Attributes object and then add this object to the element method of my ContentHandler.  Rigth now my XSL looks like the following but all I get to display is the 1st column name 4 times.  I have 4 column names at this point so I just hard coded in 4 for testing purposes.

<xsl:template match="columns">
        <fo:table-row>
            <xsl:attribute name="font-weight">bold</xsl:attribute>
            <xsl:call-template name="columnHeader">
                <xsl:with-param name="length" select="4" />
            </xsl:call-template>
        </fo:table-row>
</xsl:template>

<xsl:template name="columnHeader">
        <xsl:param name="length" />
        <xsl:if test="$length != 0">
            <fo:table-cell>
                <fo:block font-size="12pt" font-weight="bold" text-align="center">
                    <xsl:value-of select="columnNames/@*"/>
                </fo:block>
            </fo:table-cell>
            <xsl:call-template name="columnHeader">
                <xsl:with-param name="length" select="$length - 1" />
            </xsl:call-template>
        </xsl:if>
</xsl:template>

Here is my java code:

handler.startElement( "columns" );

AttributesImpl attrs = new AttributesImpl();
       
while( it.hasNext() )
{
    field = (ReportResultField)it.next();
    attrs.addAttribute( null, null, null, "columnHeader", field.getFieldName() );
}

handler.element( "columnNames", null, attrs );
handler.endElement( "columns" );
0
Comment
Question by:paulca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
pratap_r earned 125 total points
ID: 12260414
your xpath  <xsl:value-of select="columnNames/@*"/> picks up the first column 4 times,

change it to use your index parameter you are passing.
<xsl:value-of select="columnNames[$length]/@*"/>

you will be better off if you use <xsl:for-each>, in your code you will get the columns in the reverse order!

Enjoy!
Pratap
0
 

Author Comment

by:paulca
ID: 12261297
I tried the suggestion you made above and now it only displays the last column.  The 1st three columns are empty.  Not sure what I might be doing wrong.
0
 

Author Comment

by:paulca
ID: 12261671
I got it.  I had to change my java code to the following:

        handler.startElement( "columns" );
        AttributesImpl attrs = new AttributesImpl();
       
        while( it.hasNext() )
        {
            field = (ReportResultField)it.next();
            attrs.addAttribute( null, null, null, "columnHeaders", field.getFieldName() );
            handler.element( "columnNames", field.getFieldName(), attrs );
            attrs.clear();
        }
        handler.endElement( "columns" );

and my XSL to:

<xsl:for-each select="columnNames">
    <fo:table-cell>
        <fo:block font-size="12pt" font-weight="bold" text-align="center">
            <xsl:value-of select="@*"/>
        </fo:block>
    </fo:table-cell>
</xsl:for-each>

Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
This is an explanation of a simple data model to help parse a JSON feed
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question