在docbook 5.0文档中检测表元素错误 [英] Detecting table element error in docbook 5.0 document

查看:90
本文介绍了在docbook 5.0文档中检测表元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有很多运气能在我的

I did not have much luck to have a complete answer in my previous post. So I am trying yet again with a different approach.

如何使用表来检测以下docbook文章中的错误:

How would one detect the error in the following docbook article with table:

$ cat article.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<article xmlns="http://docbook.org/ns/docbook" version="5.0">
  <title>Title</title>
  <table>
    <caption>caption</caption>
    <tbody>
      <tr>
        <td rowspan="2">
          <para>my para</para>
        </td>
      </tr>
    </tbody>
  </table>
</article>

显然,由于我做了一个可重现的小例子,所以我知道输入文档文档中的错误在哪里,所以我的问题是关于可用来检测并有效跟踪它们的命令行工具(行号和列偏移量) ).

Obviously since I made a small reproducible example I know where the error is in my input docbook document, my question is instead about the command line tools that are available to detect them and tracked them down efficiently (line number and possibly column offset).

我将只接受使用输入docbook 5.0(XML)文档或输入格式设置对象(FO文件)检测到错误的答案.

I will only accept answer that detect the error either using the input docbook 5.0 (XML) document, or the input formatting-object (FO file).

答案应在一系列命令行工具中明确(使用软件"X"是不可接受的答案).

The answer should be explicit in the series of command line tools (Use software 'X' is not an acceptable answer).

理想情况下,我想我可以简单地运行:

Ideally I would have guess I could simply run:

$ xmllint --nonet --noout --schema /usr/share/xml/docbook/schema/xsd/5.0/docbook.xsd article.xml
article.xml validates

$ jing /usr/share/xml/docbook/schema/rng/5.0/docbook.rng article.xml && echo "success"
success

作为参考,这里是我在Debian GNU/Linux系统[*]上拥有的所有内容

For reference here is all I have on my Debian GNU/Linux system [*]

更新:我不是在寻找所有可能的FO问题的通用解决方案,我只是在寻找一种在长而复杂的<table>元素中跟踪问题的方法.

UPDATE: I am not looking for a general solution to all possible FO issues, I am simply looking for a way to track issue(s) in long and complex <table> element.

[*]

$ xsltproc -o article.fo /usr/share/xml/docbook/stylesheet/docbook-xsl/fo/docbook.xsl article.xml && fop article.fo article.pdf
Note: namesp. cut : stripped namespace before processing           Additional Protocols
Note: namesp. cut : processing stripped document                   Additional Protocols
Making portrait pages on USletter paper (8.5inx11in)
[WARN] FOUserAgent - The following feature isn't implemented by Apache FOP, yet: table-layout="auto" (on fo:table) (See position 2:30164)
[ERROR] FOP - Exception <org.apache.fop.apps.FOPException: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.>org.apache.fop.apps.FOPException: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
    at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:288)
    at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:115)
    at org.apache.fop.cli.Main.startFOP(Main.java:186)
    at org.apache.fop.cli.Main.main(Main.java:217)
Caused by: javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:502)
    at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:285)
    ... 3 more
Caused by: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
    at org.apache.fop.fo.flow.table.FixedColRowGroupBuilder.endTablePart(FixedColRowGroupBuilder.java:183)
    at org.apache.fop.fo.flow.table.VariableColRowGroupBuilder$6.play(VariableColRowGroupBuilder.java:107)
    at org.apache.fop.fo.flow.table.VariableColRowGroupBuilder.endTable(VariableColRowGroupBuilder.java:116)
    at org.apache.fop.fo.flow.table.Table.finalizeNode(Table.java:260)
    at org.apache.fop.fo.FONode.endOfNode(FONode.java:330)
    at org.apache.fop.fo.flow.table.Table.endOfNode(Table.java:243)
    at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:360)
    at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:190)
    at org.apache.xalan.transformer.TransformerIdentityImpl.endElement(TransformerIdentityImpl.java:1102)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at org.apache.xerces.xinclude.XIncludeHandler.endElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
    ... 4 more

---------

javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:502)
    at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:285)
    at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:115)
    at org.apache.fop.cli.Main.startFOP(Main.java:186)
    at org.apache.fop.cli.Main.main(Main.java:217)
Caused by: org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
    at org.apache.fop.fo.flow.table.FixedColRowGroupBuilder.endTablePart(FixedColRowGroupBuilder.java:183)
    at org.apache.fop.fo.flow.table.VariableColRowGroupBuilder$6.play(VariableColRowGroupBuilder.java:107)
    at org.apache.fop.fo.flow.table.VariableColRowGroupBuilder.endTable(VariableColRowGroupBuilder.java:116)
    at org.apache.fop.fo.flow.table.Table.finalizeNode(Table.java:260)
    at org.apache.fop.fo.FONode.endOfNode(FONode.java:330)
    at org.apache.fop.fo.flow.table.Table.endOfNode(Table.java:243)
    at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:360)
    at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:190)
    at org.apache.xalan.transformer.TransformerIdentityImpl.endElement(TransformerIdentityImpl.java:1102)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at org.apache.xerces.xinclude.XIncludeHandler.endElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
    ... 4 more
---------
org.apache.fop.fo.ValidationException: A table-cell is spanning more rows than available in its parent element.
    at org.apache.fop.fo.flow.table.FixedColRowGroupBuilder.endTablePart(FixedColRowGroupBuilder.java:183)
    at org.apache.fop.fo.flow.table.VariableColRowGroupBuilder$6.play(VariableColRowGroupBuilder.java:107)
    at org.apache.fop.fo.flow.table.VariableColRowGroupBuilder.endTable(VariableColRowGroupBuilder.java:116)
    at org.apache.fop.fo.flow.table.Table.finalizeNode(Table.java:260)
    at org.apache.fop.fo.FONode.endOfNode(FONode.java:330)
    at org.apache.fop.fo.flow.table.Table.endOfNode(Table.java:243)
    at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:360)
    at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:190)
    at org.apache.xalan.transformer.TransformerIdentityImpl.endElement(TransformerIdentityImpl.java:1102)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at org.apache.xerces.xinclude.XIncludeHandler.endElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
    at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:285)
    at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:115)
    at org.apache.fop.cli.Main.startFOP(Main.java:186)
    at org.apache.fop.cli.Main.main(Main.java:217)

推荐答案

我建议使用XSLT来检查您在XSL文件中定义的约束.

I propose using XSLT to check for constraints that you define in the XSL file.

我刚刚准备了一个文件来检测您提到的特定问题,可以使用xsltproc check-tables.xsl article.xml

I've just prepared such a file for detecting that specific problem you mentioned, which you can run with xsltproc check-tables.xsl article.xml

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Author: Alberto González Palomo http://sentido-labs.com
       2016-12-24 00:20 -->

  <!-- BEGIN checks -->

  <xsl:template match="db:tbody">
    <xsl:variable name="row-count" select="count(db:tr)"/>
    <xsl:for-each select=".//db:td[@rowspan]">
      <xsl:variable name="last-row-spanned" select="count(parent::db:tr/preceding-sibling::db:tr) + @rowspan"/>
      <xsl:if test="$last-row-spanned &gt; $row-count">
        <xsl:call-template name="error">
          <!-- Optional in this case:
               <xsl:with-param name="location" select="."/>
          -->
          <xsl:with-param name="message">A table-cell is spanning more rows than available in its parent element.</xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- END checks -->


  <xsl:output method="text"/>

  <xsl:template match="text()"><!-- Omit text content. --></xsl:template>

  <xsl:template name="error">
    <xsl:param name="location" select="."/>
    <xsl:param name="message">No error message available.</xsl:param>

    <!-- To stop at the first error, set the attribute terminate="yes".
         In xsltproc, this also causes the process to return a failure value.
         -->
    <xsl:message terminate="no">
      <xsl:text>Error: </xsl:text>
      <xsl:call-template name="xpath">
        <xsl:with-param name="location" select="$location"/>
      </xsl:call-template>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="$message"/>
    </xsl:message>
  </xsl:template>

  <xsl:template name="xpath">
    <xsl:param name="location" select="."/>
    <xsl:for-each select="$location/parent::*">
      <xsl:call-template name="xpath"/>
    </xsl:for-each>

    <xsl:text>/</xsl:text>
    <xsl:variable name="element-name" select="name($location)"/>
    <xsl:value-of select="$element-name"/>

    <xsl:variable name="preceding" select="count($location/preceding-sibling::*[name() = $element-name])"/>
    <xsl:variable name="following" select="count($location/following-sibling::*[name() = $element-name])"/>
    <xsl:if test="$preceding + $following &gt; 0">
      <xsl:text>[</xsl:text>
      <xsl:value-of select="1 + $preceding"/>
      <xsl:text>]</xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

"db:tbody"的模板计算可用的行数,然后遍历具有@rowspan属性的所有td元素,如果元素位置和@rowspan的总和更大,则会显示错误消息比tbody中的tr行数:

The template for "db:tbody" counts the number of rows available, then iterates over all td elements that have a @rowspan attribute and prints an error message if the sum of the element's position and the @rowspan is greater than the number of tr rows in the tbody:

错误:/article/table/tbody/tr/td:一个表格单元格跨越了更多行 比其父元素中可用的数量要多.

Error: /article/table/tbody/tr/td: A table-cell is spanning more rows than available in its parent element.

您可以通过在"db:tbody"模板中添加更多检查或为其他元素编写新模板来扩展它,并且可以通过将模板拆分到单独的XSL文件中并将其包含在<xsl:import/><xsl:include/>中来对其进行模块化.

You can extend it by adding more checks in the "db:tbody" template or by writing new templates for other elements, and you can modularize it by splitting the templates in separate XSL files and including them with <xsl:import/> or <xsl:include/>.

我们当然可以用任何其他具有XPath支持的语言来编写类似的程序,但是您已经准备好使用XSLT处理器了.

We could of course write a similar program in any other language with XPath support, but you already have an XSLT processor at hand.

这篇关于在docbook 5.0文档中检测表元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆