想要实现在docbook中,引用另外某个xml文件中的一部分,并且是带参数,或者说实体定义的,这样被引用的xml中就可以实现动态内容的替换了。
首先,xinclude部分内容的需求,已经实现了:
【已解决】Docbook中,将section从chapter中独立出去,放到单独的xml文件中
但是对于如何实现带参数的xinclude,始终没有搞定。
【解决过程】
1.原先的内容是这种,主文件:char_encoding.xml中的源码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <? xml version = '1.0' encoding = "utf-8" ?> < book version = "5.0" xml:lang = "zh_CN" > < info > < title >字符编码详解</ title > ... < abstract > < para >本文主要介绍了字符编码的基础知识,以及常见的字符编码类型,比如ASCII,Unicode,UTF-8,ISO 8859等,以及各种编码之间的关系。</ para > < note >< title >本文提供多种格式供:</ title > < informaltable align = "center" frame = "topbot" > < tgroup cols = "7" > < colspec colnum = "1" colname = "col1" colwidth = "3*" /> < colspec colnum = "2" colname = "col2" colwidth = "1*" /> < colspec colnum = "3" colname = "col3" colwidth = "1*" /> < colspec colnum = "4" colname = "col4" colwidth = "1*" /> < colspec colnum = "5" colname = "col5" colwidth = "1*" /> < colspec colnum = "6" colname = "col6" colwidth = "1*" /> < colspec colnum = "7" colname = "col7" colwidth = "1*" /> < tbody > < row > < entry >在线阅读</ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/html/char_encoding.html" >HTML</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/htmls/char_encoding.html" >HTMLs</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/pdf/char_encoding.pdf" >PDF</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/chm/char_encoding.chm" >CHM</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/txt/char_encoding.txt" >TXT</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/rtf/char_encoding.rtf" >RTF</ link ></ entry > </ row > < row > < entry >下载(7zip压缩包)</ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/html/char_encoding.html.7z" >HTML</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/htmls/char_encoding.html.7z" >HTMLs</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/pdf/char_encoding.pdf.7z" >PDF</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/chm/char_encoding.chm.7z" >CHM</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/txt/char_encoding.txt.7z" >TXT</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/rtf/char_encoding.rtf.7z" >RTF</ link ></ entry > </ row > </ tbody > </ tgroup > </ informaltable > < para >HTML版本的在线地址为:</ para > < para >< link xl:href = "https://www.crifan.com/files/doc/docbook/char_encoding/release/html/char_encoding.html" /></ para > </ note > </ abstract > < author >< personname >< firstname >Crifan</ firstname >< surname >Li</ surname ></ personname ></ author > < releaseinfo >版本:< emphasis >v1.1</ emphasis ></ releaseinfo > ... < legalnotice > < para >本文章遵从:< link xl:href = "https://www.crifan.com/files/doc/docbook/soft_dev_basic/release/html/soft_dev_basic.html#cc_by_nc" >署名-非商业性使用 2.5 中国大陆(CC BY-NC 2.5)</ link ></ para > </ legalnotice > </ info > < xi:include href = "glossary.xml" /> < xi:include href = "preface.xml" /> < xi:include href = "ch01_enc_background.xml" /> < xi:include href = "ch02_enc_type.xml" /> < xi:include href = "reference.xml" /> < xi:include href = "appendix.xml" /> </ book > |
其中的note部分的内容,其格式,对于每个book,都是一样的,只是其中的bookname不一样而已。
现在的需求是,希望把note这部分的内容提取出来,弄成一个公共common的xml文件,然后每个book去xinclude这个note部分的内容,这样以后对于note部分的声明,如果需要改动,就只需要改动common这个xml即可,而不再是现在的这种:需要每一个book都去改对应的xml文件,而目前有20多个book,改动多个文件的做法,效率很低。
2.目前,经过折腾,已实现了
对于主文件docbook_dev_note.xml,改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <? xml version = '1.0' encoding = "utf-8" ?> < book version = "5.0" xml:lang = "zh_CN" > < info > < title >Docbook开发手记</ title > ... < abstract > < para >本文主要介绍了Docbook开发过程中,遇到的环境搭建,常见问题以及解决办法,以及其他一些注意事项和基本知识点</ para > </ abstract > < author >< personname >< firstname >Crifan</ firstname >< surname >Li</ surname ></ personname ></ author > < releaseinfo >版本:< emphasis >v1.0</ emphasis ></ releaseinfo > ... < legalnotice > < para >本文章遵从:< link xl:href = "https://www.crifan.com/files/doc/docbook/soft_dev_basic/release/html/soft_dev_basic.html#cc_by_nc" >署名-非商业性使用 2.5 中国大陆(CC BY-NC 2.5)</ link ></ para > </ legalnotice > </ info > < xi:include href = "preface.xml" /> < xi:include href = "ch01_build_env.xml" /> ...... </ book > |
然后对应的公共部分的内容是放在common.xml中的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <? xml version = '1.0' encoding = "utf-8" ?> <!DOCTYPE book [ <!ENTITY % crl_ent PUBLIC "crl.ent" 'null'>%crl_ent; <!ENTITY cur_book_name "docbook_dev_note" > ]> < note xml:id = "common_note" > < title >本文提供多种格式供:</ title > < informaltable align = "center" frame = "topbot" > < tgroup cols = "7" > < colspec colnum = "1" colname = "col1" colwidth = "3*" /> < colspec colnum = "2" colname = "col2" colwidth = "1*" /> < colspec colnum = "3" colname = "col3" colwidth = "1*" /> < colspec colnum = "4" colname = "col4" colwidth = "1*" /> < colspec colnum = "5" colname = "col5" colwidth = "1*" /> < colspec colnum = "6" colname = "col6" colwidth = "1*" /> < colspec colnum = "7" colname = "col7" colwidth = "1*" /> < tbody > < row > < entry >在线阅读</ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/html/&cur_book_name;.html" >HTML</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/htmls/&cur_book_name;.html" >HTMLs</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/pdf/&cur_book_name;.pdf" >PDF</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/chm/&cur_book_name;.chm" >CHM</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/txt/&cur_book_name;.txt" >TXT</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/rtf/&cur_book_name;.rtf" >RTF</ link ></ entry > </ row > < row > < entry >下载(7zip压缩包)</ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/html/&cur_book_name;.html.7z" >HTML</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/htmls/&cur_book_name;.html.7z" >HTMLs</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/pdf/&cur_book_name;.pdf.7z" >PDF</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/chm/&cur_book_name;.chm.7z" >CHM</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/txt/&cur_book_name;.txt.7z" >TXT</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/rtf/&cur_book_name;.rtf.7z" >RTF</ link ></ entry > </ row > </ tbody > </ tgroup > </ informaltable > < para >HTML版本的在线地址为:</ para > < para >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/html/&cur_book_name;.html" /></ para > </ note > |
但是离最终目标,对于每个book都可以xinclude这个common文件,然后xinclude的时候,把cur_book_name传递进去的目标,还差不少。
3.也想过,是否可以利用xml的template去实现一个common的template,但是后来发现,好像xml的template,是用于生成xsl文件的,而不是用于我这种从xml得到xml的情况的。
4.后来又想到了,是否可以利用之前docbook包含的entity的xml文件来包含common这个xml。
然后折腾了一下,发现是可以的。
具体实现方法是:
主文件中定义对应的变量,然后xinclude对应common的xml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <? xml version = '1.0' encoding = "utf-8" ?> <!DOCTYPE book [ <!ENTITY cur_book_name "docbook_dev_note" > <!ENTITY common_note SYSTEM "common_note.xml"> ]> < book version = "5.0" xml:lang = "zh_CN" > < info > < title >Docbook开发手记</ title > < subtitle ></ subtitle > < abstract > < para >本文主要介绍了Docbook开发过程中,遇到的环境搭建,常见问题以及解决办法,以及其他一些注意事项和基本知识点</ para > <!-- <xi:include href="common.xml" xpointer="common_note" xmlns:xi="http://www.w3.org/2001/XInclude" /> --> &common_note; </ abstract > ... </ info > ... </ book > |
然后common_note.xml中就是对应xml声明以及相应内容了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <? xml version = '1.0' encoding = "utf-8" ?> < note xml:id = "common_multi_format" > < title >本文提供多种格式供:</ title > < informaltable align = "center" frame = "topbot" > < tgroup cols = "7" > < colspec colnum = "1" colname = "col1" colwidth = "3*" /> < colspec colnum = "2" colname = "col2" colwidth = "1*" /> < colspec colnum = "3" colname = "col3" colwidth = "1*" /> < colspec colnum = "4" colname = "col4" colwidth = "1*" /> < colspec colnum = "5" colname = "col5" colwidth = "1*" /> < colspec colnum = "6" colname = "col6" colwidth = "1*" /> < colspec colnum = "7" colname = "col7" colwidth = "1*" /> < tbody > < row > < entry >在线阅读</ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/html/&cur_book_name;.html" >HTML</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/htmls/&cur_book_name;.html" >HTMLs</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/pdf/&cur_book_name;.pdf" >PDF</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/chm/&cur_book_name;.chm" >CHM</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/txt/&cur_book_name;.txt" >TXT</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/rtf/&cur_book_name;.rtf" >RTF</ link ></ entry > </ row > < row > < entry >下载(7zip压缩包)</ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/html/&cur_book_name;.html.7z" >HTML</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/htmls/&cur_book_name;.html.7z" >HTMLs</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/pdf/&cur_book_name;.pdf.7z" >PDF</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/chm/&cur_book_name;.chm.7z" >CHM</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/txt/&cur_book_name;.txt.7z" >TXT</ link ></ entry > < entry >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/rtf/&cur_book_name;.rtf.7z" >RTF</ link ></ entry > </ row > </ tbody > </ tgroup > </ informaltable > < para >HTML版本的在线地址为:</ para > < para >< link xl:href = "https://www.crifan.com/files/doc/docbook/&cur_book_name;/release/html/&cur_book_name;.html" /></ para > </ note > |
由此,就可以实现了,带参数的包含xml子文件的效果。
【总结】
通过xinclude子文件的话,没法实现对应的带参数的效果,或者说带实体定义的效果,但是可以通过entity定义文件的方法,实现此带参数的包含子xml的效果。