开发者

Adding line break in a PDF with XSL-FO?

Trying to create a PDF file based on an XML and a file using XMLSpy.

I'm trying to split a field into two lines based on the fie开发者_如何学Pythonld content.

For example, if my varialbe = "John Doe AKA Johnny D", I want to view it like this :

John Doe

Johnny D

My problem is that I can't make it work even with all the samples on the net.

Here's my code :

     <xsl:value-of disable-output-escaping="yes" select="concat(substring-before(//MyField,'AKA'),$newline,substring-after(//MyField,'AKA'))" /> 
  </xsl:when>

So basically, eveytime I find the "AKA" string, I want to break the field into two lines. So my code, finds the string, create the new variable but still shows in one line. I've tried creating a variable with a blank line, using all kinds of techiniques but still shows in one line.

Any thoughts ?


See my answer here about using a hex entity reference and linefeed-treatment.


Edit

I took your code from the comments and put it in template in a sample XSLT stylesheet. The only thing I changed was:

  1. I changed your newline variable to &#xA;.
  2. I added linefeed-treatment="preserve" to your fo:block.

Using a dummy XML file and the XSLT stylesheet, I produced an XSL-FO document that when rendered with FOP, produces "John Doe" and "Johnny D" on separate lines.

Here is the XML file:

<doc>
  <MyField>John Doe AKA Johnny D</MyField>
</doc>

Here is the XSLT stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
          <fo:region-body margin="1in" margin-top="1.5in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="doc">
    <xsl:variable name="newline" select="'&#xA;'"/>        
    <xsl:variable name="MyVar">
      <xsl:choose>
        <xsl:when test="contains(//MyField,'AKA')">
          <xsl:value-of select="concat(substring-before(//MyField,'AKA'),$newline,substring-after(//MyField,'AKA'))"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="//MyField"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <fo:block linefeed-treatment="preserve">
      <xsl:value-of select="$MyVar"/>
    </fo:block>
  </xsl:template>

</xsl:stylesheet>

Here is the resulting XSL-FO:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:root>
            <fo:layout-master-set>
               <fo:simple-page-master page-height="11in" page-width="8.5in" master-name="my-page">
                  <fo:region-body margin-top="1.5in" margin="1in"/>
               </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="my-page">
               <fo:flow flow-name="xsl-region-body">
                  <fo:block linefeed-treatment="preserve">John Doe 
 Johnny D</fo:block>
               </fo:flow>
            </fo:page-sequence>
         </fo:root>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

The PDF is a single 8.5" x 11" page with this on it:

John Doe
Johnny D


The answer by @daniel-haley still produces a single pair of names when the source is:

<doc>
    <MyField>John Doe AKA Johnny D</MyField>
    <MyField>Johnny D AKA John Doe</MyField>
    <MyField>John Smith</MyField>
</doc>

(In XPath 1.0, converting a node-set to a string returns the string value of only the node in the node-set that is first in document order. See https://www.w3.org/TR/xpath/#function-string.)

The stylesheet below splits any text node containing "AKA". Since the enclosing fo:block comes from the xsl:template for MyField, this version generates an empty fo:block to cause the line break.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                <fo:region-body margin="1in" margin-top="1.5in" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="my-page">
            <fo:flow flow-name="xsl-region-body">
                <xsl:apply-templates />
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

<!-- Could change to match on 'MyField/text()[contains(., ' AKA ')]'
     if necessary. -->
<xsl:template match="text()[contains(., ' AKA ')]">
    <xsl:value-of select="substring-before(., ' AKA ')" />
    <fo:block />
    <xsl:value-of select="substring-after(., ' AKA ')" />
</xsl:template>

<xsl:template match="MyField">
    <fo:block>
        <xsl:apply-templates />
    </fo:block>
</xsl:template>

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜