Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Transforming Java Objects to XML and Displaying Using XSL

Posted on 2004-10-08
3
Medium Priority
?
297 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 500 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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…
Simple Linear Regression

783 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