从 XML 在 XSL 中生成 table

Generate table in XSL from XML

我想制作一个 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 表示为 Table,但我不知道如何在现有文件中实现它。

The Result should look like this

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

很多问候

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

<?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>

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

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