从XML

时间:2018-05-01 11:53:43

标签: xml xslt-2.0 xsl-fo polarion

我想创建一个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>
&#13;
&#13;
&#13;

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

The Result should look like this

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

许多问候

1 个答案:

答案 0 :(得分: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"/>(再次使用任何所需的名称空间前缀) 1}}元素。