XML - XSL - FO XML Dot Leaders Mutliple Occurances

时间:2015-07-30 14:40:04

标签: xml xslt xsl-fo leader

我有数百行文本,格式化为XML文档,我需要输出到PDF并显示为:

LEFT TEXT.......................................................................RIGHT TEXT

我发现如何在XSL中执行此操作,但它在FO输出期间当前作为处理指令运行。 XSL:

<xsl:template match="processing-instruction('leftrighttext')">
  <fo:block text-align-last="justify">
    <xsl:text>LEFT TEXT</xsl:text>
    <fo:leader leader-pattern="dots"/>
    <xsl:text>RIGHT TEXT</xsl:text>
  </fo:block>

XML的样子(在表中使用):

<row>
<entry> <?leftrighttext?> </entry>
</row

期望的输出:

LEFT TEXT........................................................................RIGHT TEXT
NEXT TEXT WITHIN TABLE...........................................................OTHER TEXT

我的问题是如何从XML文档中调用“LEFT TEXT”和“RIGHT TEXT”,而不是直接在样式表中输入值,例如:

<row>
<entry>LEFT TEXT <sometag> RIGHT TEXT</entry>
</row>
<row>
<entry>NEXT TEXT WITHIN TABLE <sometag> OTHER TEXT

我可以创建一个标签,或者在标签之前标识文本的字符串,以及之后的文本,在它们之间输入点头?

任何帮助将不胜感激。

要求的其他信息: 我在下面的答案中使用了XSLT,这里是我试图转换的XML文档。我还按照下面发布的XML的大纲。请让我知道我错过了什么...

XML:

<?xml-model href="http://docbook.org/xml/5.0/rng/docbook.rng" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="http://docbook.org/xml/5.0/rng/docbook.rng" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
    version="5.0">    
    <table>
        <tgroup cols="2">
            <colspec colnum="1" colname="col1" colwidth="235"/>
            <colspec colnum="2" colname="col2" colwidth="235"/>
            <thead>
                <row>
                    <entry align="center">LSP</entry>
                    <entry align="center">RSP</entry>
                </row>
            </thead>
            <tbody>
                <row>
                    <entry align="center" namest="col1" nameend="col2">(PF) Takeoff Briefing<?leftrighttext?>PERFORM</entry>
                </row>
            </tbody>
        </tgroup>
    </table>
</section>

这就是样式表开头的内容:

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:d="http://docbook.org/ns/docbook"
exclude-result-prefixes="xs date"
extension-element-prefixes="date"
version="1.0">
<xsl:import href="http://docbook.sourceforge.net/release/xsl-ns/current/fo/docbook.xsl"/>

1 个答案:

答案 0 :(得分:4)

我会坚持使用处理指令的原始方法。您只需使用xsl:apply-templates来构建fo:table-cell

示例:

XML输入

<doc>
    <row>
        <entry>LEFT TEXT <?leftrighttext?> RIGHT TEXT</entry>
    </row>
    <row>
        <entry>NEXT TEXT WITHIN TABLE <?leftrighttext?> OTHER TEXT</entry>
    </row>    
</doc>

XSLT 1.0

<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" margin-bottom="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:table>
                        <fo:table-body>
                            <xsl:apply-templates select="row"/>                            
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="row">
        <fo:table-row>
            <xsl:apply-templates select="entry"/>
        </fo:table-row>
    </xsl:template>

    <xsl:template match="entry">
        <fo:table-cell>
            <fo:block text-align-last="justify">
                <xsl:apply-templates/>
            </fo:block>
        </fo:table-cell>
    </xsl:template>

    <xsl:template match="entry/text()">
        <xsl:value-of select="normalize-space()"/>
    </xsl:template>

    <xsl:template match="processing-instruction('leftrighttext')">
        <fo:leader leader-pattern="dots"/>
    </xsl:template>

</xsl:stylesheet>

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" margin-bottom="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:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align-last="justify">LEFT TEXT<fo:leader leader-pattern="dots"/>RIGHT TEXT</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align-last="justify">NEXT TEXT WITHIN TABLE<fo:leader leader-pattern="dots"/>OTHER TEXT</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

呈现PDF

enter image description here

更新示例

查看更新XML示例,我看到您有一个默认命名空间(xmlns="http://docbook.org/ns/docbook")。您应该做的是在XSLT中使用前缀声明该命名空间,然后在XPath中使用该前缀。

如果您正在使用XSLT 2.0,则可以将xpath-default-namespace="http://docbook.org/ns/docbook"添加到xsl:stylesheet,而不是更改任何XPath。

很难推荐具体的东西,因为你还没有展示完整的XSLT(甚至是一个向我们展示tbody创建位置的上下文的模板),所以这是一个完整的猜测。

以下是使用更新的XML并使用前缀http://docbook.org/ns/docbook声明doc命名空间的示例。

(我没有尝试处理表格标题或类似的东西只是为了保持简单。)

XML输入

<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
    version="5.0">    
    <table>
        <tgroup cols="2">
            <colspec colnum="1" colname="col1" colwidth="235"/>
            <colspec colnum="2" colname="col2" colwidth="235"/>
            <thead>
                <row>
                    <entry align="center">LSP</entry>
                    <entry align="center">RSP</entry>
                </row>
            </thead>
            <tbody>
                <row>
                    <entry align="center" namest="col1" nameend="col2">(PF) Takeoff Briefing<?leftrighttext?>PERFORM</entry>
                </row>
            </tbody>
        </tgroup>
    </table>
</section>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:doc="http://docbook.org/ns/docbook" 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" margin-bottom="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:table">
        <fo:table>
            <fo:table-body>
                <xsl:apply-templates select="doc:tgroup/doc:tbody/doc:row"/>                            
            </fo:table-body>
        </fo:table>        
    </xsl:template>

    <xsl:template match="doc:row">
        <fo:table-row>
            <xsl:apply-templates select="doc:entry"/>
        </fo:table-row>
    </xsl:template>

    <xsl:template match="doc:entry">
        <fo:table-cell>
            <fo:block text-align-last="justify">
                <xsl:apply-templates/>
            </fo:block>
        </fo:table-cell>
    </xsl:template>

    <xsl:template match="doc:entry/text()">
        <xsl:value-of select="normalize-space()"/>
    </xsl:template>

    <xsl:template match="processing-instruction('leftrighttext')">
        <fo:leader leader-pattern="dots"/>
    </xsl:template>

</xsl:stylesheet>

XSL-FO输出

<fo:root xmlns:doc="http://docbook.org/ns/docbook"
         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" margin-bottom="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:table>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell>
                     <fo:block text-align-last="justify">(PF) Takeoff Briefing<fo:leader leader-pattern="dots"/>PERFORM</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

呈现PDF

enter image description here