1

我想制作一个 XSL 文件来生成 PDF 文件。

         <values>
            <item>
              <html>
                <p xmlns="http://www.w3.org/1999/xhtml">Step</p>
              </html>
            </item>
            <item>
              <html>
                <p xmlns="http://www.w3.org/1999/xhtml">Description</p>
              </html>
            </item>
            <item>
              <html>
                <p xmlns="http://www.w3.org/1999/xhtml">Result</p>
              </html>
            </item>
          </values>

不,我想将我的 PDF 表示为表格,但我不知道如何在现有文件中实现它。

结果应该是这样的

我真的是 XSL 的新手,我希望有人能帮助解决这个问题。

许多问候

4

1 回答 1

2

将该 XML 片段转换为 XSL-FO 表的最小示例是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="fo xhtml"
    version="3.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="sample">
                <fo:region-body/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="sample">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    <xsl:apply-templates/>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>

</xsl:template>

<xsl:template match="values">
    <fo:table>
        <fo:table-column column-width="*"/>
        <fo:table-column column-width="*"/>
        <fo:table-column column-width="*"/>
        <fo:table-header>
          <fo:table-row>
            <fo:table-cell>
              <fo:block font-weight="bold">Step</fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-weight="bold">Step description</fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block font-weight="bold">Expected result</fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-header>

        <fo:table-body>
          <fo:table-row>
              <xsl:apply-templates/>
          </fo:table-row>
        </fo:table-body>
    </fo:table>
</xsl:template>

<xsl:template match="item">
   <fo:table-cell>
      <fo:block>
          <xsl:value-of select="html/xhtml:p"/>
      </fo:block>
    </fo:table-cell>
</xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/gWmuiJ4将您的输入片段转换为 FO

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="sample">
         <fo:region-body/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="sample">
      <fo:flow flow-name="xsl-region-body">
         <fo:block>
            <fo:table>
               <fo:table-column column-width="*"/>
               <fo:table-column column-width="*"/>
               <fo:table-column column-width="*"/>
               <fo:table-header>
                  <fo:table-row>
                     <fo:table-cell>
                        <fo:block font-weight="bold">Step</fo:block>
                     </fo:table-cell>
                     <fo:table-cell>
                        <fo:block font-weight="bold">Step description</fo:block>
                     </fo:table-cell>
                     <fo:table-cell>
                        <fo:block font-weight="bold">Expected result</fo:block>
                     </fo:table-cell>
                  </fo:table-row>
               </fo:table-header>
               <fo:table-body>
                  <fo:table-row>
                     <fo:table-cell>
                        <fo:block> i am a step</fo:block>
                     </fo:table-cell>
                     <fo:table-cell>
                        <fo:block>i am a desc</fo:block>
                     </fo:table-cell>
                     <fo:table-cell>
                        <fo:block>i am a res</fo:block>
                     </fo:table-cell>
                  </fo:table-row>
               </fo:table-body>
            </fo:table>
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

呈现为表格,您将需要添加属性以具有边框。

要将这种方法插入到现有样式表中,您将考虑输入名称空间并为匹配表达式或选择表达式添加前缀,就像您在样式表的其他部分中所做的那样,然后您只需将最后两个模板插入到您的样式表并确保在要在元素父级上下文中插入表格的位置使用<xsl:apply-templates/>或(同样,使用任何需要的命名空间前缀)。<xsl:apply-templates select="values"/>values

于 2018-05-01T12:22:30.137 回答