【问题标题】:Update attribute in XSL for-each更新 XSL 中的属性 for-each
【发布时间】:2010-10-11 00:25:27
【问题描述】:

在工作中,我接到了使用 XSL 生成 PDF 的有趣任务。我正在使用的 XML 结构类似于

<records>
    <topLevel>
        <topLevelID></topLevelID>
        <secondLevel>
            <secondLevelID></secondLevelID>
            <thirdLevel>
            </thirdLevel>
            <thirdLevel>
            </thirdLevel>
        </secondLevel>
    </topLevel>
    <topLevel>
        <topLevelID></topLevelID>
        <secondLevel>
            <secondLevelID></secondLevelID>
            <thirdLevel>
            </thirdLevel>
            <thirdLevel>
            </thirdLevel>
        </secondLevel>
    </topLevel>
</records>

我会尝试给出一个更有意义的 XML 示例,但我不想触及任何可能存在的法律界限。使用这种 XML 结构,我必须在 PDF 中为每个 thirdLevel 节点输出一个文本块。我到目前为止的 XSL 就像

<xsl:for-each select ="topLevel">          
    <xsl:variable name="topID" select="topLevelID"/>
    <xsl:for-each select ="secondLevel">
        <xsl:variable name="secondID" select="secondLevelID"/>
        <xsl:for-each select="thirdLevel">            
            <fo:block-container position="absolute" height="12.8pt" width="220.8pt" left="160pt" display-align="auto">
                <xsl:attribute name="top">
                    <xsl:value-of select="concat(193 + [whatshouldgohere]), 'pt')"/>
                </xsl:attribute>
                <fo:block font-size="7pt">                          
                    <xsl:call-template name="insertThirdLevel"/>
                </fo:block>
            </fo:block-container>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

基本上,我需要为 top 属性添加一些值,以使每个 thirdLevel 节点的文本出现在自己的行上。我尝试过使用 ID 的加/乘组合(从 1 开始,每组加 1)和position(),但我似乎无法正确处理。

【问题讨论】:

  • "concat((193*position()), 'pt')" 应该可以,你是怎么做乘法的?
  • 我遇到的问题是,我基本上有三个不同的位置,可以用于三个不同的 for-each 级别。此外,由于数据在 XML 中的布局方式,仅使用 position() 似乎有时会导致文本输出在其他文本之上。

标签: pdf xslt pdf-generation xsl-fo


【解决方案1】:

我认为你真的应该研究一下&lt;xsl:apply-templates&gt;,它可以为你节省很多打字时间。

简化版:

<xsl:variable name="line-height" select="10" />

<xsl:template match="/records">
  <xsl:apply-templates select="//thirdLevel" />
</xsl:template>

<xsl:template match="thirdLevel">
  <xsl:variable name="top" select="193 + position() * $line-height" />
  <fo:block-container top="{concat($top , 'pt')}">
    <fo:block font-size="7pt">                          
      <xsl:call-template name="insertThirdLevel"/>
    </fo:block>
  </fo:block-container>
</xsl:template>

<xsl:template name="insertThirdLevel">
  Third Level!
</xsl:template>

简化输出(“fo”命名空间除外):

<fo:block-container top="203pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="213pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="223pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>
<fo:block-container top="233pt">
  <fo:block font-size="7pt">
    Third Level!
  </fo:block>
</fo:block-container>

【讨论】:

  • 谢谢!我想太多了。感谢您为我简化它。
猜你喜欢
  • 2020-02-14
  • 2011-10-30
  • 2012-10-11
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
相关资源
最近更新 更多