2016-12-13 76 views
0

我沒有太多的運氣在我的previous post中有完整的答案。所以我正在嘗試一種不同的方法。檢測docbook 5.0文檔中的表格元素錯誤

如何將一個檢測與table下面的DocBook文章中的錯誤:

$ 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> 

很顯然,因爲我做了一個小的重複的例子,我知道那裏的錯誤是我的輸入DocBook文檔中,我的問題是不是有關命令行工具可用於檢測它們並有效地追蹤它們(行號和可能的列偏移量)。

我只接受使用輸入docbook 5.0(XML)文檔或輸入格式化對象(FO文件)來檢測錯誤的答案。

答案應該在一系列命令行工具中明確(使用軟件「X」不是可接受的答案)。

理想的情況下我會想我可以簡單地運行:

$ 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系統上[*]


更新:我不是尋找所有可能的FO問題的一般解決方案,我只是尋找一種方式來跟蹤我在冗長而複雜的元素中出現。

[*]

$ 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) 
+1

請參閱http://stackoverflow.com/questions/18998498/validate-fo-file/19001319#19001319上的答案,並參閱https://www.oxygenxml.com/forum/topic7924.html#p24197:「The XSL-FO的問題在於沒有工具可以完全正確地驗證它。XML Schema無法指定XSL-FO輸出文件中的所有約束...... *對我來說,最好的驗證是將XSL-FO處理爲PDF,從而讓處理器在遇到*時發出警告和錯誤*。「儘管我意識到FOP的限制是不給出源中錯誤的行+列。 – sideshowbarker

+0

您的示例表格不是CALS表格,但鏈接到http://tdg.docbook.org/tdg/5.0/cals.table.html。 – mzjn

+0

我能想到的唯一工具是Schematron。使用Schematron規則來檢測錯誤的跨度值可能是可能的。 – mzjn

回答

1

我建議使用XSLT來檢查您在XSL文件中定義的約束。

我剛準備這樣的文件在所有td元素檢測你提到的具體問題,您可以與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屬性和如果該元素的位置和@rowspan的總和大於tr排在tbody的數量大輸出錯誤消息:

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/>將它們包括在內來對其進行模塊化。

我們當然可以使用XPath支持的其他語言編寫類似的程序,但您已經擁有了XSLT處理器。

+0

'count(parent :: db:tr/preceding-sibling :: db:tr)'看起來不對因爲你認爲'@ rowspan'在這種情況下等於1。 – malat

+0

@malat嗨,計數給出了問題之前的行數,然後我們得到了@ @ rowspan的值。所以,如果在這個之前有5行,而這個行的長度是3,那麼最後一行的從1開始的索引將是5 + 3 = 8。 –

+0

@malat再次,也許你的意思是表達式假定當前行之前的所有行都有一個行跨度。 這很好,因爲根據HTML標準[html],行大於1的當前行上方的單元格不會將任何單元格向下推,而是向右。 '@ rowspan'不會在任何地方添加行,否則fop不會抱怨,只是添加任何額外的行。 [html] https://www.w3.org/TR/html401/struct/tables.html#h-11.2.6.1 –