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

  • Home
  • SEARCH
  • 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 8524331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:39:29+00:00 2026-06-11T07:39:29+00:00

I have the following XML: <sample> <value1>This is one</value1> <value2>Number two</value2> <value4>Last value</value4> </sample>

  • 0

I have the following XML:

<sample>
    <value1>This is one</value1>
    <value2>Number two</value2>
    <value4>Last value</value4>
</sample>

Using Apache FOP/XSL-FO I would like a PDF looking similar to this:

Value 1: This is one Value 2: Number two
Value 3:                 Value 4: Last value

Notice the spacing/padding between “Value 3:” and “Value 4:”.

The following transformation gives my the result I want. But it seems overly complicated (and might not perform very well for a real-life PDF with many values).

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:template match="sample">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="page-layout">
                    <fo:region-body margin="10mm" region-name="body"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="page-layout">
                <fo:flow flow-name="body">
                    <fo:block>
                        <xsl:variable name="pad">                            
                            <xsl:choose>
                                <xsl:when test="value1">5</xsl:when>
                                <xsl:otherwise>25</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <fo:inline padding-right="{$pad}mm">Value 1: <xsl:value-of select="value1"/></fo:inline>
                        Value 2: <xsl:value-of select="value2"/>
                    </fo:block>
                    <fo:block>
                        <xsl:variable name="pad">
                            <xsl:choose>
                                <xsl:when test="value3">5</xsl:when>
                                <xsl:otherwise>25</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <fo:inline padding-right="{$pad}mm">Value 3: <xsl:value-of select="value3"/></fo:inline>
                        Value 4: <xsl:value-of select="value4"/>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

</xsl:stylesheet>

Is there a simpler/better way to implement this?

Update:

I refactored the above into a template “field”:

<xsl:template name="field">
    <xsl:param name="txt"/>
    <xsl:param name="val"/>
    <xsl:param name="pad"/>
    <xsl:variable name="p">
        <xsl:choose>
            <xsl:when test="$val">5</xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="$pad"><xsl:value-of select="$pad"/></xsl:when>
                    <xsl:otherwise>25</xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <fo:inline padding-right="{$p}mm"><xsl:value-of select="$txt"/>:</fo:inline>
    <fo:inline keep-with-previous="always" padding-right="5mm" font-weight="bold"><xsl:value-of select="$val"/></fo:inline>
</xsl:template>

Which can be used like this:

<xsl:call-template name="field">
    <xsl:with-param name="txt">Value 1</xsl:with-param>
    <xsl:with-param name="val"><xsl:value-of select="sample/value1"/></xsl:with-param>
</xsl:call-template>

The template takes a third optional parameter, pad. If specified its value will be used as padding.

David’s template (see accepted answer) uses a simpler if-contruct where the padding-right attribute is overwritten if needed.

  • 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-11T07:39:31+00:00Added an answer on June 11, 2026 at 7:39 am

    Since this presumably isn’t being executed in a browser is there any reason not to use xslt2 (which would make it rather nicer) since you are using java anyway the free saxon 9 implementation would work well here.

    But here’s an XSLT 1 version refactored a bit.

    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    
        <xsl:template match="sample">
            <fo:root>
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="page-layout">
                        <fo:region-body margin="10mm" region-name="body"/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="page-layout">
                    <fo:flow flow-name="body">
             <xsl:call-template name="twoval">
              <xsl:with-param name="a" select="value1"/>
              <xsl:with-param name="ahead" select="'Value 1'"/>
              <xsl:with-param name="b" select="value2"/>
              <xsl:with-param name="bhead" select="'Value 2'"/>
             </xsl:call-template>
             <xsl:call-template name="twoval">
              <xsl:with-param name="a" select="value3"/>
              <xsl:with-param name="ahead" select="'Value 3'"/>
              <xsl:with-param name="b" select="value4"/>
              <xsl:with-param name="bhead" select="'Value 4'"/>
             </xsl:call-template>
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
    
    
    <xsl:template name="twoval">
    <xsl:param name="a"/>
    <xsl:param name="ahead"/>
    <xsl:param name="b"/>
    <xsl:param name="bhead"/>
    <fo:block>
     <fo:inline padding-right="25mm">
      <xsl:if test="$a"><xsl:attribute name="padding-right">5mm</xsl:attribute></xsl:if>
      <xsl:value-of select="$ahead"/><xsl:text>: </xsl:text>
      <xsl:value-of select="$a"/>
     </fo:inline>
     <xsl:text> </xsl:text>
     <xsl:value-of select="$bhead"/><xsl:text>: </xsl:text>
     <xsl:value-of select="$b"/>
    </fo:block>
    </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML: <xmlRoot> <DomainName>sample.com</DomainName> <UserCountsByTemplate> <KeyValueOfstringint> <Key>default</d2p1:Key> <Value>20</d2p1:Value> </KeyValueOfstringint> <KeyValueOfstringint> <Key>basic</d2p1:Key>
I have the following XML <ns0:Root xmlns:ns0=http://Map_Sample.Input_Schema> <Number1> </Number1> <Number2>11</Number2> <Number3>12</Number3> </ns0:Root> In this
I have following xml and I want to fetch the value of node which
I have the following xml sample: <items> <item> <item_id>1</item_id> <item_name>item 1</item_name> <group_id>1</group_id> <group_name>group 1</group_name>
I have the following XML example that I need some help on. The sample
Following is sample XML File - <?xml version=1.0 encoding=UTF-8?> <Catalog> <Book> <AName>Steven Holzner</AName> <BName>Using
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>
I have following XML, and i wish to save its data in my SQL
I have following XML. I was able to remove all namespaces but not able
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>

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.