【问题标题】:How to show all XSL elements?如何显示所有 XSL 元素?
【发布时间】:2017-07-11 15:06:13
【问题描述】:

XML 文件包含帐户和帐户列表(包含 ID 和 AccountDescription)。 在下面的示例中,有 2 个帐户。

<?xml version="1.0"?>
<Accounts>
 <Account>
  <ID>5</ID>
  <AccountDescription>Account Description 5</AccountDescription>
 </Account>
 <Account>
  <ID>8</ID>
  <AccountDescription>Account Description 8</AccountDescription>
  </Account>
</Accounts>

当使用下面的 XSL 时,它会创建一个包含 2 页的 PDF 文件,每个页面都有标题 ID 和 AccountDescription,但它下面没有数据/内容,如下所示:

在第 1 页:

ID 帐户描述

在第 2 页:

ID 帐户描述

我想这样显示数据:

ID 帐户描述

5 账户说明 5

8 帐户说明 8

我该怎么做?谢谢。

这是我当前的 XSL:

    <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="Accounts">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">  
  <fo:layout-master-set>
          <fo:simple-page-master 
              master-name="main"
              margin-top="0px"
              margin-bottom="0px"
              margin-left="18px"
              margin-right="18px">
              <fo:region-body margin-top="0.75in" margin-bottom="2in" margin-left="18px" margin-right="18px"/>
              <fo:region-before extent="0.75in"/>
              <fo:region-after extent="1.5in"/>
              <fo:region-end extent="75px"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <xsl:apply-templates select="Account"/>
</fo:root>
</xsl:template>

<xsl:template match="Account">
<fo:page-sequence master-reference="main">
    <fo:flow flow-name="xsl-region-body">
        <fo:table font-size="10pt">
            <fo:table-column column-width="15mm"/>
            <fo:table-column column-width="55mm"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell >
                        <fo:block text-align="right"><xsl:value-of select="ID"/></fo:block>
                    </fo:table-cell>
                    <fo:table-cell >
                        <fo:block text-align="right"><xsl:value-of select="AccountDescription"/></fo:block>
                    </fo:table-cell>
                    </fo:table-row>
            </fo:table-body>
        </fo:table>
    </fo:flow>

</fo:page-sequence>
</xsl:template>

</xsl:stylesheet>

【问题讨论】:

  • 您正在尝试将模板应用于/匹配AccountRow,但该元素在您的示例 XML 中不存在。这是您的样式表的问题还是您的示例的问题?
  • 是的,你是对的。我编辑了我的原始帖子,所以现在我没有调用 。相反,我使用 。有了这个,我确实看到了数据,但是每个数据都显示在不同的页面上,即:第 1 页我有 5 - 帐户描述 5,在第 2 页我有 8 - 帐户描述 8。我怎样才能在同一页面中显示所有数据页 ?谢谢。

标签: asp.net xml asp.net-mvc-4 xslt apache-fop


【解决方案1】:

如何在同一页面中显示所有数据?

您只需使用一个fo:page-sequence。将其从Account 模板向上移动到Accounts 模板中。

更新的 XSLT

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <xsl:template match="Accounts">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">  
      <fo:layout-master-set>
        <fo:simple-page-master 
          master-name="main"
          margin-top="0px"
          margin-bottom="0px"
          margin-left="18px"
          margin-right="18px">
          <fo:region-body margin-top="0.75in" margin-bottom="2in" margin-left="18px" margin-right="18px"/>
          <fo:region-before extent="0.75in"/>
          <fo:region-after extent="1.5in"/>
          <fo:region-end extent="75px"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="main">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates select="Account"/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="Account">
    <fo:table font-size="10pt">
      <fo:table-column column-width="15mm"/>
      <fo:table-column column-width="55mm"/>
      <fo:table-body>
        <fo:table-row>
          <fo:table-cell >
            <fo:block text-align="right"><xsl:value-of select="ID"/></fo:block>
          </fo:table-cell>
          <fo:table-cell >
            <fo:block text-align="right"><xsl:value-of select="AccountDescription"/></fo:block>
          </fo:table-cell>
        </fo:table-row>
      </fo:table-body>
    </fo:table>
  </xsl:template>

</xsl:stylesheet>

【讨论】:

  • 行得通,谢谢!但是,它为每一行重复列标题。所以,我对其进行了细微的更改:ID账户描述表体>
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 2021-05-26
  • 2010-10-20
相关资源
最近更新 更多