最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】docbook中使用xsltproc出错:Element xxx in namespace xxx encountered in book, but no template matches

Docbook crifan 2712浏览 0评论

docbook 5中使用xsltproc,出现上述问题。

下面记录了相关的折腾过程:

1.问题:

Element preface in namespace ” encountered in book, but no template matches.
Element title in namespace ” encountered in preface, but no template matches.
Element section in namespace ” encountered in preface, but no template matches.
Element title in namespace ” encountered in section, but no template matches.

。。。。。

原因:

在xml中,不能同时存在:

<!DOCTYPE book [
<!ENTITY preface SYSTEM "preface.xml">
<!ENTITY chap1 SYSTEM "mpeg_related.xml">
<!ENTITY chap2 SYSTEM "mp3_term.xml">
<!ENTITY chap3 SYSTEM "mpeg_frame.xml">
<!ENTITY chap4 SYSTEM "calc_mp3_method.xml">
<!ENTITY chap5 SYSTEM "how_calc.xml">
<!ENTITY chap6 SYSTEM "mp3_repated.xml">
<!ENTITY append SYSTEM "append.xml">
<!ENTITY refer SYSTEM "refer.xml">
]>

和<book>中带XInclude:

<book xmlns="http://docbook.org/ns/docbook" version="5.0"
xmlns:xlink=’http://www.w3.org/1999/xlink’
xmlns:xi="http://www.w3.org/2001/XInclude"
xml:lang="zh-CN">

办法:

删除<!DOCTYPE book部分的内容,然后引用外部xml(其他chapter)的时候,用:

xmlns:xi="http://www.w3.org/2001/XInclude"

<xi:include href="preface.xml"/>

搭配使用。

2.问题:

Element include in namespace ‘http://www.w3.org/2001/XInclude’ encountered in book, but no template matches.
Element include in namespace ‘http://www.w3.org/2001/XInclude’ encountered in book, but no template matches.

原因:

xi:include的位置不对,放在了<bookinfo>之外。

办法:

把xi:include放到<bookinfo>内,和<title>并列:

<book xmlns="http://docbook.org/ns/docbook" version="5.0"
xmlns:xlink=’http://www.w3.org/1999/xlink’
xmlns:xi="http://www.w3.org/2001/XInclude"
xml:lang="zh-CN">

<bookinfo>
<title>MPEG简介 + 如何计算CBR和VBR的MP3的播放时间</title>

<xi:include href="preface.xml"/>
<xi:include href="mpeg_related.xml"/>

<author><firstname>Crifan</firstname><affiliation><orgname>crifan.com</orgname></affiliation><email>admin AT crifan DOT com</email></author>
。。。。。。

【后记】

虽然这样是可以让编译通过了,但是实际上生成的pdf并没有包含对应内容。

所以,还是有问题的。

参考:Conversion from docbook to PDF or HTML

Can’t create PDF from book chunk with includes

但是还是不能解决。

也试了添加-xinclude,但是还是出错:

Administrator@K470 /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src
$ xsltproc.exe –xinclude -o /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/output/fo/MPEG_VBR.fo /usr/share/sgml/docbook/xsl-ns- stylesheets/fo/docbook_fo_cr_yahei.xsl /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src/MPEG_VBR.xml
Making portrait pages on USletter paper (8.5inx11in)
Element preface in namespace ” encountered in book, but no template matches.
Element title in namespace ” encountered in preface, but no template matches.
Element section in namespace ” encountered in preface, but no template matches.
Element title in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element section in namespace ” encountered in preface, but no template matches.
Element title in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element para in namespace ” encountered in section, but no template matches.
Element chapter in namespace ” encountered in book, but no template matches.
Element title in namespace ” encountered in chapter, but no template matches.
Element info in namespace ” encountered in chapter, but no template matches.
Element sect1 in namespace ” encountered in chapter, but no template matches.
Element title in namespace ” encountered in sect1, but no template matches.
Element para in namespace ” encountered in sect1, but no template matches.
Element sect1 in namespace ” encountered in chapter, but no template matches.
Element title in namespace ” encountered in sect1, but no template matches.
Element para in namespace ” encountered in sect1, but no template matches.
Element sect1 in namespace ” encountered in chapter, but no template matches.
Element title in namespace ” encountered in sect1, but no template matches.
Element para in namespace ” encountered in sect1, but no template matches.
Element orderedlist in namespace ” encountered in sect1, but no template matches.
Element listitem in namespace ” encountered in orderedlist, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element listitem in namespace ” encountered in orderedlist, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element listitem in namespace ” encountered in orderedlist, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.

其中,可以看到,我的此处所包含的两个xml中,一个是preface,一个是chapter,两个都无法match,都无法正常编译。

后来,找了N个帖子,但是都没解决我的问题:

(1)一堆帖子,说是要添加 –xinclude参数即可的:

DOCBOOK 转化为HTML,chm遇到的问题,请留步看一下

docbook-apps message

Conversion from docbook to PDF or HTML

但是我这里加了–xinclude的后还是有类似错误。

(2)也有人不小心把2001写成2003的:

Re: Mathml and xslt

后经证实,这个网址:

http://www.w3.org/2001/XInclude

是可以打开的,

http://www.w3.org/2003/XInclude

也可以打开,但是人家已经提示2003的namespace已经废弃了,让你用2001的。

所以,正确的做法,就是引用2001才对。

(3)这哥们:

Gallio and MbUnit v3.04

是高手,都自己搞定源代码的,但是我这里是直接用编译后的工具,没有源码,有源码估计也很难改对地方和编译出来正确的工具。所以,没借鉴意义。

(4)这个:Can’t create PDF from book chunk with includes,是添加了什么“<xsl:template match="xi:include">:”的东西,没看懂什么怎么用的。所以也没法参考其做法了。

最后经历了千辛万苦的折腾,都快绝望要放弃的时候,无意间发现,将原先的:

<preface>
<title>正文之前</title>
……..
</preface>

改为:

<preface xmlns="http://docbook.org/ns/docbook"
xml:id="preface"
xmlns:xi="http://www.w3.org/2001/XInclude">

<title>正文之前</title>
……..
</preface>

错误就只剩这些了:

Administrator@K470 /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src
$ xsltproc.exe –xinclude -o /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/output/fo/MPEG_VBR.fo /usr/share/sgml/docbook/xsl-ns- stylesheets/fo/docbook_fo_cr_yahei.xsl /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src/MPEG_VBR.xml
Making portrait pages on USletter paper (8.5inx11in)
Element chapter in namespace ” encountered in book, but no template matches.
Element title in namespace ” encountered in chapter, but no template matches.
Element info in namespace ” encountered in chapter, but no template matches.
Element sect1 in namespace ” encountered in chapter, but no template matches.
Element title in namespace ” encountered in sect1, but no template matches.
Element para in namespace ” encountered in sect1, but no template matches.
Element sect1 in namespace ” encountered in chapter, but no template matches.
Element title in namespace ” encountered in sect1, but no template matches.
Element para in namespace ” encountered in sect1, but no template matches.
Element sect1 in namespace ” encountered in chapter, but no template matches.
Element title in namespace ” encountered in sect1, but no template matches.
Element para in namespace ” encountered in sect1, but no template matches.
Element orderedlist in namespace ” encountered in sect1, but no template matches.
Element listitem in namespace ” encountered in orderedlist, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element listitem in namespace ” encountered in orderedlist, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element listitem in namespace ” encountered in orderedlist, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.
Element para in namespace ” encountered in listitem, but no template matches.

所以又照葫芦画瓢,改了mpeg_related.xml,从:

<chapter>

<title>MPEG的相关知识</title>
……..
</chapter>

变为:

<chapter xmlns="http://docbook.org/ns/docbook"
xml:id="mpeg_related"
xmlns:xi="http://www.w3.org/2001/XInclude">

<title>MPEG的相关知识</title>
……..
</chapter>

然后就完全解决了问题,编译就没有任何错误了:

Administrator@K470 /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src
$ xsltproc.exe –xinclude -o /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/output/fo/MPEG_VBR.fo /usr/share/sgml/docbook/xsl-ns- stylesheets/fo/docbook_fo_cr_yahei.xsl /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src/MPEG_VBR.xml
Making portrait pages on USletter paper (8.5inx11in)

然后接着生成pdf:

Administrator@K470 /cygdrive/e/Dev_Root/docbook/dev/src/books/VBR/src
$ E:/Dev_Root/docbook/config/tool/fop/fop-1.0/fop.cmd -c E:/Dev_Root/docbook/config/tool/fop/fop-1.0/conf/fop.xconf E:/Dev_Root/d ocbook/dev/src/books/VBR/output/fo/MPEG_VBR.fo -pdf E:/Dev_Root/docbook/dev/src/books/VBR/output/pdf/MPEG_VBR.pdf
四月 29, 2012 10:31:24 下午 org.apache.fop.apps.FopFactoryConfigurator configure
信息: Default page-height set to: 11in
四月 29, 2012 10:31:25 下午 org.apache.fop.apps.FopFactoryConfigurator configure
信息: Default page-width set to: 8.26in
四月 29, 2012 10:31:26 下午 org.apache.fop.events.LoggingEventListener processEvent
警告: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
四月 29, 2012 10:31:26 下午 org.apache.fop.events.LoggingEventListener processEvent
警告: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".
四月 29, 2012 10:31:26 下午 org.apache.fop.hyphenation.Hyphenator getHyphenationTree
严重: Couldn’t find hyphenation pattern en

生成的pdf,就包含了所希望的,preface和第一章的内容了:

can include xml content

至此,终于算是解决了那个“but no template matches”的问题。

【总结】

1. 问题:

Element include in namespace ‘http://www.w3.org/2001/XInclude’ encountered in book, but no template matches.

原因:

没有包含对应的你的外部的xml文件,比如我上面的preface.xml和mpeg_related.xml。

办法:

在使用xsltproc的时候,添加–xinclude参数 –> 这样就可以将你的外部的xml文件包含进来了。

2.问题:

Element preface in namespace ” encountered in book, but no template matches.

Element chapter in namespace ” encountered in book, but no template matches.

原因:

当前这个xml文件中,没有包含对应的XInclude

办法:

将xml文件最开始的中的:

<chapter>

改为:

<chapter xmlns="http://docbook.org/ns/docbook" 
xml:id="mpeg_related" 
xmlns:xi="http://www.w3.org/2001/XInclude">

即可。类似的,preface也一样处理即可。

【相关思考】

之前就是因为看了官方文档:

2. Physical Divisions: Breaking a Document into Separate Files

其中提到,对于外部xml,千万不要再加DOCTYPE声明了:

But it must not begin with its own Document Type Declaration:

  1 <!DOCTYPE chapter>
  2 <chapter xmlns="http://docbook.org/ns/docbook"
             xml:id="ch1">
  4 <title>My First Chapter</title>
    <para>My first paragraph.</para>
  6 …

但是实际上,我此处,却是:

对于

<!DOCTYPE chapter>

<!DOCTYPE preface>

加与不加,都无所谓。

而对于:

xmlns="http://docbook.org/ns/docbook"

xml:id="preface"

xmlns:xi=http://www.w3.org/2001/XInclude

则必须要添加,否则就会出现上述的那个“but no template matches”错误的。

所以,貌似看起来,和官方文档的解释,不太一致,甚至算是冲突啊。

真不知道怎么搞的。

后来搞懂了,原来人家说的是,如果用:

<!ENTITY chap1 SYSTEM "chap1.xml">

的形式的话,则外部xml则不能带上述标识,而此处是XInclude,则是可以的。

3.问题:

org.apache.fop.apps.FOPException: org.apache.fop.fo.ValidationException: Property ID "toc.id8956905.mpeg_related" (found on "fo:block") previously used; ID values must be unique within a document! (See position 3:17020)

javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: Property ID "toc.id8956905.mpeg_related" (found on "fo:block") previously used; ID values must be unique within a document! (See position 3:17020)

原因:

新建的一个xml文件,其xml的id:

xml:id="mpeg_related"

与之前的某xml的id一样:

xml:id="mpeg_related"

解决办法:

确保每一个xml文件中,xml的id,都是不一样的。

此处我的错误主要是由于新建xml是基于旧的xml的拷贝,所以忘了改xml的id了。

转载请注明:在路上 » 【已解决】docbook中使用xsltproc出错:Element xxx in namespace xxx encountered in book, but no template matches

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.165 seconds, using 22.35MB memory