繁体   English   中英

从XML在XSL中生成表

[英]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表示为表格,但我不知道如何在现有文件中实现它。

结果应如下所示

我在XSL中真的很新,我希望有人可以帮助解决这个问题。

许多问候

将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元素的父级的上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM