Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:07:27+00:00 2026-06-10T22:07:27+00:00

I am trying to implement FOP to output a PDF using XML and XSLT

  • 0

I am trying to implement FOP to output a PDF using XML and XSLT files.

My problem is the following I need to fix the position of words in a line (but not through using tables) for example:

I have the following xml:

 <address>
    <Line1 length="32" noLine="5" col="60" />
    <Line2 length="32" noLine="6" col="60">Mr. John Kane</Line2 >
    <Line3 length="32" noLine="7" col="60">15 Street Springfield</Line3 >
    <Line4 length="32" noLine="8" col="60" />
    <Line5 length="32" noLine="9" col="60" />
    <Line6 length="6" noLine="10" col="60">75009</Line6 >
    <Line7 length="25" noLine="10" col="67">Freesberg</Line7 >
    <Line8 length="25" noLine="11" col="67">Idaho</Line8 >
  </address>
  1. Where the length is the word/sentence length
  2. noLine is the line number
  3. col is the beginning position of the word/sentence in the line

I did the lines but I can’t seem to get to insert the word/sentence in the right position (col) in the line.

This is a part from my xslt:

<fo:block font-size="10" font-family="monospace">
<xsl:for-each select="*">
<xsl:variable name="currentNode" select ="name(.)"/>

<xsl:choose>
<xsl:when test="$currentNode = 'address'">
                                                <xsl:for-each select="*">
                                                    <xsl:variable name="length" select ="@length"/>
                                                    <xsl:variable name="noLine" select ="@noLine"/>
                                                    <xsl:variable name="col" select ="@col"/>
                                                    <xsl:variable name="precNoLig" select = "preceding-sibling::*[1]/@noLine"/>
                                                    <xsl:choose>
                                                        <xsl:when test="$precNoLig = $noLine">
                                                            <fo:block font-size="10" font-family="monospace" text-indent="60">
                                                                &#160;<xsl:value-of select="." />
                                                            </fo:block>
                                                        </xsl:when>
                                                        <xsl:otherwise>
                                                            <!--<fo:block font-size="10" font-family="monospace" >-->
                                                                &#x2028;<xsl:value-of select="." />
                                                            <!--</fo:block>-->
                                                        </xsl:otherwise>
                                                    </xsl:choose>
                                                </xsl:for-each>
    </xsl:when>
    </xsl:choose>

    </xsl:for-each>
    </fo:block>

This is the expected output as PDF :

                                         Mr. John Kane
                                         15 Street Springfield


                                         75009 Freesberg
                                               Idaho

Where it has the following positions in the PDF (col):

<-----------------60-------------------->
<-----------------60-------------------->Mr. John Kane
<-----------------60-------------------->15 Street Springfield
<-----------------60-------------------->
<-----------------60-------------------->
<-----------------60-------------------->75009 Freesberg
<-----------------67-------------------------->Idaho

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:07:29+00:00Added an answer on June 10, 2026 at 10:07 pm

    It’s a little convoluted, but this should work:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:strip-space elements="*" />
      <xsl:output method="text"/>
    
      <xsl:variable name="sp" select="'                                                                                          '" />
    
      <xsl:template match="address/*">
        <xsl:variable name="value">
          <xsl:value-of select="." />
          <xsl:if test="following-sibling::*[1]/@noLine = @noLine">
            <xsl:value-of select="$sp" />
          </xsl:if>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="preceding-sibling::*[1]/@noLine = @noLine">
            <xsl:variable name="col" select="preceding-sibling::*[1]/@col + preceding-sibling::*[1]/@length" />
            <xsl:value-of select="concat(substring($sp,1,@col - $col),substring($value,1,@length))" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="preceding-sibling::*"><xsl:text>&#10;</xsl:text></xsl:if>
            <xsl:value-of select="concat(substring($sp,1,@col),substring($value,1,@length))" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>
    

    For this transform to work as-is, it’s critical that your Line.. elements are in noLine/col order, as it bases how to pad it out on the previous element. If they’re out of order it’ll get it badly wrong.

    The sp variable must contain at least as many spaces as you’ll ever need to pad it out to, i.e. the max value of any length or col attribute. This solution doesn’t include spaces at the end of a line- it’s actually easier to pad out each line with trailing spaces so that it fills the length given, but I assumed it would probably be preferable not to.

    I’ve only tried it with the sample given, if it doesn’t work for any other input you’ve got, let me know and I’ll see if I can adapt it.

    EDIT: I just noticed that you needed it in PDF format, sorry. I just looked at the output, and didn’t notice the format. Hopefully you can adapt this to the format you need (It’s actually quite similar to what you’d already tried), but if not, I’d suggest you manually create a PDF that represents your output and add the XML for that PDF to your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement Piwik using REST API over http but need a little help.
I am trying implement Unblock me Puzzle. i want to change image position from
Trying to implement search with Sunspot Gem wich is using Solr.Fulltext search works fine
I'm trying implement a way to recursively template using jsRender. The issue is, my
Trying to implement the following structure from c to use NSArray in objective-c: In
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Trying to implement following queries in NHibernate QueryOver. SQL SELECT SUM(GrossChargeAmount) FROM Charges INNER
Trying to implement an autocomplete box ultimately. For now im following php academy's lead.
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.