【问题标题】:Error exporting PDF with FOP "fo:list-item" is not a valid child of "fo:list-item-body"!使用 FOP 导出 PDF 时出错“fo:list-item”不是“fo:list-item-body”的有效子代!
【发布时间】:2015-11-26 20:11:30
【问题描述】:

我们在尝试使用 fop 导出 PDF 时遇到错误:

严重:例外 org.apache.fop.apps.FOPException: org.apache.fop.fo.ValidationException: "fo:list-item" 不是 "fo:list-item-body" 的有效子代! (没有可用的上下文信息) javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: "fo:list-item" 不是 "fo:list-item-body" 的有效子代! (没有可用的上下文信息)

据我所知,我们的 XSL 看起来应该可以运行。这是 XSL 的摘录。

 <xsl:template match="ol" mode="section-body">
    <fo:list-block keep-together="always">
    <fo:list-item>
        <fo:list-item-label>
        </fo:list-item-label>
        <fo:list-item-body>
        <xsl:apply-templates select="li" mode="section-body" />
        </fo:list-item-body> 
    </fo:list-item>      
    </fo:list-block>
  </xsl:template>

  <xsl:template match="ul" mode="section-body">
    <fo:list-block provisional-distance-between-starts="40mm" content-width="2.5in">
    <fo:list-item>
            <fo:list-item-label>
            </fo:list-item-label>
            <fo:list-item-body>
                <xsl:apply-templates select="li" mode="section-body"/>
            </fo:list-item-body>
    </fo:list-item> 
    </fo:list-block>
  </xsl:template>

  <xsl:template match="li" mode="section-body">
    <xsl:element name="fo:list-item">
      
      <xsl:if test="position() = last()">
        
      </xsl:if>
      <fo:list-item-label>
        <xsl:choose>
          <xsl:when test="parent::ol">
            <xsl:element name="fo:block" use-attribute-sets="list-marker-number"><xsl:value-of select="concat(position(),'.')" /></xsl:element>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="fo:block" use-attribute-sets="list-marker">•</xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </fo:list-item-label>
      <fo:list-item-body>
        <xsl:choose>
          <xsl:when test="parent::ol">
            <xsl:element name="fo:block" use-attribute-sets="list-item-number">
              <xsl:apply-templates mode="section-body"/>
            </xsl:element>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="fo:block">
              <xsl:apply-templates mode="section-body"/>
            </xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </fo:list-item-body>
    </xsl:element>
  </xsl:template>

【问题讨论】:

    标签: java xml apache xsl-fo apache-fop


    【解决方案1】:

    olul 的模板都生成:

    <fo:list-block keep-together="always">
    <fo:list-item>
        <fo:list-item-label>
        </fo:list-item-label>
        <fo:list-item-body>
        <-- Result of processing <li> goes here! -->
        </fo:list-item-body> 
    </fo:list-item>      
    </fo:list-block>
    

    每个li 都会生成一个fo:list-item,其中包含一个fo:list-item-label 和一个fo:list-item-body

    如果您从olul 模板中删除fo:list-itemfo:list-item-labelfo:list-item-body

    <xsl:template match="ol" mode="section-body">
      <fo:list-block keep-together="always">
          <xsl:apply-templates select="li" mode="section-body" />
      </fo:list-block>
    </xsl:template>
    
    <xsl:template match="ul" mode="section-body">
      <fo:list-block provisional-distance-between-starts="40mm" content-width="2.5in">
                <xsl:apply-templates select="li" mode="section-body"/>
      </fo:list-block>
    </xsl:template>
    

    那么您将不会在fo:list-item-body 中生成fo:list-item

    如果您想尝试在 FOP 之外验证您的 FO,那么我建议 focheck https://github.com/AntennaHouse/focheck

    【讨论】:

    • 您能否建议我在使用 Apache fop 和 POJO 生成 pdf 时如何迭代对象的 ArrayList。
    • 我建议您对此提出一个新问题,看看是否有人可以为您解答。
    • 是的。我有一个问题,如果你想看看stackoverflow.com/questions/47675189/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多