问题
Consider the DocBook article in test.xml which contains an <informaltable>
that is repeated at the beginning of each section. Consider also that the <informaltable>
is actually much more complicated than this example shows.
This example accomplishes reuse of the complicated <informaltable>
using an external <!ENTITY ... SYSTEM ...>
declaration. The complicated <informaltable>
is in another file called reusedtable.xml.
test.xml
<!DOCTYPE article [<!ENTITY ReusedTable SYSTEM "reusedtable.xml">]>
<article xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
<info>
<title>Article Template Title</title>
</info>
<section>
<title>first title</title>
&ReusedTable;
</section>
<section>
<title>Second Title</title>
&ReusedTable;
</section>
</article>
reusedtable.xml
The file that contains the reused table.
<informaltable>
<tgroup cols='2'>
<tbody>
<row>
<entry>YES</entry>
<entry>NO</entry>
</row>
</tbody>
</tgroup>
</informaltable>
Here's what the output looks like
This method works, but it seems a bit awkward and limited. So it leaves me with the following questions:
- Is there a way to accomplish the reuse of my
<informaltable>
without creating a second .xml file? - Is there a way to accomplish the reuse of my
<informaltable>
so that I could parameterize the table?
For example, I'd like to be able to express the presence of an instance of ReusedTable populated with different content in my docbook article like this,
test2.xml
<article xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
<info>
<title>Article Template Title</title>
</info>
<section>
<title>first title</title>
<ReusedTable>
<firstcol>true</firstcol>
<seccol>false</seccol>
</ReusedTable> </section>
<section>
<title>Second Title</title>
<ReusedTable>
<firstcol>yes</firstcol>
<seccol>no</seccol>
</ReusedTable>
</section>
</article>
and have the published output look like this where the design of ReusedTable is defined once and the content of the cells in each instance of ReusedTable comes from markup in the article where the table will appear.
回答1:
Probably a better way to include complex tables (or other parts) is through XInclude see also this question and answer Can ENTITY declarations be nested in referenced XML files? As each tables content is different, there is no way to dynamically have that content updated, during the render process. If this indeed is what you need, then a way to solve the issue, is generate the (complex) tables, through a small program, as separate xml files. And include these separate xml files through XInclude in your document.
回答2:
- Is there a way to accomplish the reuse of my
<informaltable>
without creating a second .xml file?
Yes, also by declaring an entity but putting the <informaltable>
inline by omitting the SYSTEM
keyword in the declaration. This eliminates the need for another file altogether. Test.xml from the original question looks like this with the entity declarations inline:
Test.xml
<!DOCTYPE article [
<!ENTITY ReusedTable "
<informaltable>
<tgroup cols='2'>
<tbody>
<row>
<entry>YES</entry>
<entry>NO</entry>
</row>
</tbody>
</tgroup>
</informaltable>
">
]>
<article xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
<info>
<title>Article Template Title</title>
</info>
<section>
<title>first title</title>
&ReusedTable;
</section>
<section>
<title>Second Title</title>
&ReusedTable;
</section>
</article>
来源:https://stackoverflow.com/questions/11680142/is-there-a-better-method-than-entity-to-reuse-a-complicated-table-in-docbook