Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2007, 09:26 AM
Registered User
 
Join Date: Aug 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default FO Question

Bear with me, I'm new at FO...
I'm creating a PDF and I need to display several data items, with labels, on the same line. Example:

Label 1: Data1 Label 2: Data2 Label 3: Data3

The XML looks something like this:
<root>
<DATA>
<data1>value1</data1>
<data2>value2</data2>
<data3>value3</data3>
</DATA>
</root>

I'm using xsl that looks like this: (I've left all the page setup)

<xsl:template match="DATA">
 <xsl:apply-templates/>
</xsl:template>


<xsl:template match="data1">
 <fo:block (left out some formatting)>
        <xsl:value-of select="'Label 1: '"/>
        <xsl:value-of select="."/>
</fo:block>
</xsl:template>

If I add another template:
<xsl:template match="data2">
 <fo:block (left out some formatting)>
        <xsl:value-of select="'Label 2: '"/>
        <xsl:value-of select="."/>
</fo:block>
</xsl:template>

the subsequent data is placed on a new line.
Maybe I'm trying to hard, but I don't want to use a table, and I think the templates that process the data items should be "standalone" if that make sense. I know I could match on <DATA> and then put everything in the same fo:block element. But is this the best way? Thanks,
 
Old August 21st, 2007, 09:42 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm no FO expert but shouldn't you have the fo:block element declared in the template that matches DATA and then process the data* elements?

--

Joe (Microsoft MVP - XML)
 
Old August 21st, 2007, 10:15 AM
Registered User
 
Join Date: Aug 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried something like this:

<xsl:template match="DATA">
       <fo:block font-size="14pt"
                font-family="Times Roman"
                line-height="18pt"
                space-after.optimum="30pt"
                background-color="white"
                color="black"
                text-align="left">
 <xsl:apply-templates select="data1"/>
 <xsl:apply-templates select="data2"/>
</fo:block>
</xsl:template>

<xsl:template match="data1">
<fo:inline
            text-align="left"
        <xsl:value-of select="'label 1: '"/>
        <xsl:value-of select="."/>
</fo:inline>
</xsl:template>

<xsl:template match="data2">
<fo:inline
            text-align="left"
            start-indent="10cm">
        <xsl:value-of select="'label 2: '"/>
        <xsl:value-of select="."/>
</fo:inline>
</xsl:template>

This places the text/data on the same line, but jams it together:
label 1:<data1>label 2:<data2>

Can't figure out how to space it over. I know I can use a table (like I might for HTML) but would rather "do it right."
 
Old August 21st, 2007, 10:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sorry that's too much of an FO question for me, I would set the margin and or padding on the elements in HTML. I don't however consider using a table incorrect for presentation of data as this is. The usual objection to tbales is when they are used solely for layout reasons.

--

Joe (Microsoft MVP - XML)
 
Old August 30th, 2007, 05:17 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're trying to hard to avoid making a table. If you want everything to be on the same line, just make a simple table and set the cell padding to the spacing value you want.

Also, it would be easier to iterate through the data subfields (ie. data1, data2,... dataN) if you gave them all the same tag and distinguished them by attributes. Not only does it simplify your schema/DTD and prevents validation problems, but then you could use something like the following:

Something like
<xsl:template match="DATA">
<fo:table>
<fo:table-body>
<fo:table-row>
<xsl:for-each select="data">
<fo:table cell xsl:use-attribute-sets="cell-style">
<fo:block>
<xsl:text>Label </xsl:text>
<xsl:value-of select="@datanumber" />
<xsl:text>: </xsl:text>
<xsl:value-of select="." />
<fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
<fo:table-body>
<fo:table>
</xsl:template>

The attribute 'xsl:use-attribute-sets' in the fo: tags refers to attribute sets like the following

<xsl:attribute-set name="cell-style">
<xsl:attribute name="padding-start">2pt</xsl:attribute>
<xsl:attribute name="padding-end">2pt</xsl:attribute>
</xsl:attribute-set>


Hope that helps.

lvnrvltn
si phi
 
Old September 4th, 2007, 11:52 AM
Registered User
 
Join Date: Aug 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. I already had some some data specific attributes in addition to the element names, so I just need to match on <DATA>, without looping. I finally wound up using a simple table. As no one offered a different solution, I assumed this was the canonical method. I like your formatting method, similar to class values for HTML cells. I'll use it the next time.






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL-FO NEO1976 XML 2 July 19th, 2006 10:10 AM
xsl:fo with Select Birger XSLT 4 July 18th, 2006 11:38 AM
XSL-FO Question gray XSLT 1 February 18th, 2005 11:31 AM
how to have a selection of action pages for one fo gilgalbiblewheel Java GUI 0 September 17th, 2004 08:32 AM
hello, some problems with XSL-FO LuisMM XSLT 4 December 19th, 2003 02:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.