Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Transforming Java Objects to XML and Displaying Using XSL

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

722 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