Solved

Transforming Java Objects to XML and Displaying Using XSL

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

820 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