1

我正在尝试为我的 pdf 文件做斑马条纹。XML如下:

<root>
   <order>
      <attribute1>1</attribute1>
      <attribute2>2</attribute2>
      <attribute3>0</attribute3>
      <attribute4>4</attribute4>
      <attribute5/>
   </order>
</root>

如果值为“0”,则不显示属性 3。如果没有值,attribute5 也不会出现。所以我不能像下面那样做斑马条纹:

<fo:table-row (colored)>

   <fo:table-cell>
     <fo:block>
       <xsl:text>Attribute1</xsl:text>
      </fo:block>
   </fo:table-cell>

   <fo:table-cell>
     <fo:block>
       <xsl:text>...</xsl:text>
      </fo:block>
   </fo:table-cell>
</fo:table-row>

<fo:table-row (non colored)>
   <fo:table-cell>
     <fo:block>
       <xsl:text>Attribute2</xsl:text>
      </fo:block>
   </fo:table-cell>

   <fo:table-cell>
     <fo:block>
       <xsl:text>...</xsl:text>
      </fo:block>
   </fo:table-cell>
</fo:table-row>

因为attribute3和attribute5并不总是出现在pdf文件中。我该怎么做?

4

1 回答 1

2

您需要在这里做的是首先使用xsl:apply-templates仅选择您希望输出的子节点(假设您当前位于order元素上:

<xsl:apply-templates select="*[normalize-space()][. != '0']" />

然后你有一个模板来匹配order元素的子元素,如下所示:

在此模板中,您可以输出表格行,并执行“彩色”属性,您可以测试当前属性的“位置”,看它是奇数还是偶数:

<fo:table-row>
  <xsl:if test="position() mod 2 = 0">
    <xsl:attribute name="colour">zebra</xsl:attribute>
  </xsl:if>

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="order">
    <fo:table>
      <xsl:apply-templates select="*[normalize-space()][. != '0']" />
    </fo:table>
  </xsl:template>

  <xsl:template match="order/*">
    <fo:table-row>
      <xsl:if test="position() mod 2 = 0">
        <xsl:attribute name="colour">zebra</xsl:attribute>
      </xsl:if>
      <fo:table-cell>
        <fo:block>
          <xsl:value-of select="local-name()" />
        </fo:block>
      </fo:table-cell>
      <fo:table-cell>
        <fo:block>
          <xsl:text>...</xsl:text>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>
</xsl:stylesheet>

显然,您将在此处使用正确的 xsl-fo 样式,而不是此处显示的字面上的 'colour=zebra' 属性....

于 2013-09-03T12:06:30.057 回答