Solved

Transforming Java Objects to XML and Displaying Using XSL

Posted on 2004-10-08
3
285 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now