ENTITY声明可以嵌套在引用的XML文件中吗? [英] Can ENTITY declarations be nested in referenced XML files?
问题描述
我正在处理一个相当大的DocBook XML文档。主书中有各章,但通过使用实体进行引用,包括了所有小节。像这样的东西:
main.book.xml
:
<!DOCTYPE图书PUBLIC-// OASIS // DTD DocBook XML V4.4 // EN http://www.oasis-open.org/docbook/ xml / 4.4 / docbookx.dtd
[
<!ENTITY section1 SYSTEM ../fragments/section1.xml\">
<!ENTITY section2 SYSTEM ../fragments/section2.xml\">
<!ENTITY section3 SYSTEM ../fragments/section3.xml\">
<!ENTITY section3_a SYSTEM ../fragments/section3_a.xml\">
<!ENTITY section3_b SYSTEM ../fragments/section3_b.xml\">
<!ENTITY section3_c SYSTEM ../fragments/section3_c.xml\">
]>
< book>
< chapter>
< title>第1章< / title>
& section1;
& section2;
& section3;
< / chapter>
< / book>
第3节又分为另外三个xml文件,其内容通过引用被包含如下: / p>
section3.xml
:
<?xml version = 1.0 encoding = UTF-8?>
< section id = Section3>
< title>第3部分< / title>
& section3_a;
& section3_b;
& section3_c;
< / section>
问题:有没有办法移动仅由第3节使用的ENTITY声明(即section3_a,section3_b ,等等)到 section3.xml
而不是在 main.book.xml
中声明它们?
是的,这是可能的,只需将它们添加到文档中即可使用。
但是我强烈不鼓励使用实体,包括其他文档(部分)!迟早您将陷入困境,该文档(部分)中的一个(或多个)不可用。您的文档将不会呈现,并且搜索引起该问题的问题非常麻烦。更好的解决方案是使用XInclude来包含文档。
带有ENTITY条目的解决方案
<?xml version = 1.0 encoding = UTF-8?>
<!DOCTYPE书PUBLIC-// OASIS // DTD DocBook XML V4.4 // EN
http://www.oasis-open.org/docbook/xml/4.4/ docbookx.dtd
[
<!ENTITY section1 SYSTEM ../fragments/section1.xml\">
<!ENTITY section2 SYSTEM ../fragments/section2.xml\">
<!ENTITY section3 SYSTEM ../fragments/section3.xml\">
]>
< book>
< chapter>
< title>第1章< / title>
& section1;
& section2;
& section3;
< / chapter>
< / book>
和其他文档文件:
<?xml version = 1.0 encoding = UTF-8?>
<!DOCTYPE书PUBLIC-// OASIS // DTD DocBook XML V4.4 // EN
http://www.oasis-open.org/docbook/xml/4.4/ docbookx.dtd
[
<!ENTITY section3_a SYSTEM ../fragments/section3_a.xml\">
<!ENTITY section3_b SYSTEM ../fragments/section3_b.xml\">
<!ENTITY section3_c SYSTEM ../fragments/section3_c.xml\">
]>
< section id = Section3>
< title>第3部分< / title>
& section3_a;
& section3_b;
& section3_c;
< / section>
提示:您甚至可以将所有实体一起移出文档,请参阅我在这个问题上写的答案 DocBook宏?
使用XInclude的解决方案
这里是一个如何使用XInclude设置文档的示例。实体条目用于小字符串。并使用XInclude进行文件包含。
<?xml version ='1.0'encoding ='UTF-8'?> ;
<!DOCTYPE书PUBLIC-// OASIS // DTD DocBook XML V4.4 // EN
http://www.oasis-open.org/docbook/ xml / 4.4 / docbookx.dtd [
<!ENTITY maven.project.version(未设置)>
<!ENTITY%实体系统 entities.ent>
%实体;
]>
< book>
< title>& product.name;< / title>
< subtitle>发行说明< / subtitle>
< bookinfo>
< date>& product.release.date;< / date>
< releaseinfo><?eval $ {project.version}?>< / releaseinfo>
< / bookinfo>
< ;!-包括章节->
< xi:include xmlns:xi = http://www.w3.org/2001/XInclude href = changes / chapter-release-2.0.xml />
< xi:include xmlns:xi = http://www.w3.org/2001/XInclude href = changes / chapter-release-2.1.xml />
< / book>
章节文件(放入更改目录
),如上一文档中所述。如果如上所述使用 xi:include
,如果无法解析 href
属性,它将停止渲染。
<?xml version = 1.0 encoding = UTF-8?>
<!DOCTYPE章PUBLIC-// OASIS // DTD DocBook XML V4.4 // EN
http://www.oasis-open.org/docbook/ xml / 4.4 / docbookx.dtd [
<!ENTITY部分更改 section-changes-v2.0.xml>
<!ENTITY%实体SYSTEM ../entities.ent\">
%实体;
]>
<章节id = release-2.0>
< title> Release 2.0< / title>
< para>
本章中提供的信息适用于< emphasis>& product.name;。 v2.0 。
< / para>
< xi:include xmlns:xi = http://www.w3.org/2001/XInclude href =& section-changes;>找不到
< xi:fallback>< para> FIXME文件:& section-changes;< / para>< / xi:fallback>
< / xi:include>
< / chapter>
此处 xi:include
与后备,因此如果无法解决 xi:include
的 href
属性,则将后备呈现为文档(这将显示一个带有FIXME的段落。这里实际上使用一个实体来引用文档(位置)。这很棒,因为该引用随后可以在 href $ c $中使用) c>和FIXME部分!
请谨慎参考实体文件 ../ entities.ent
如果无法解决,再次会带来令人讨厌的错误。
I'm working on a rather large DocBook XML document. The main book has the chapters but includes all the subsections by reference using entities. Something like this:
main.book.xml
:
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"
[
<!ENTITY section1 SYSTEM "../fragments/section1.xml">
<!ENTITY section2 SYSTEM "../fragments/section2.xml">
<!ENTITY section3 SYSTEM "../fragments/section3.xml">
<!ENTITY section3_a SYSTEM "../fragments/section3_a.xml">
<!ENTITY section3_b SYSTEM "../fragments/section3_b.xml">
<!ENTITY section3_c SYSTEM "../fragments/section3_c.xml">
]>
<book>
<chapter>
<title>Chapter 1</title>
§ion1;
§ion2;
§ion3;
</chapter>
</book>
Section 3 is in turn divided into three more xml files whose content is included by reference like so:
section3.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<section id="Section3">
<title>Section 3</title>
§ion3_a;
§ion3_b;
§ion3_c;
</section>
QUESTION: Is there a way to move the ENTITY declarations used only by Section 3 (i.e. section3_a, section3_b, etc) to section3.xml
instead of declaring them in main.book.xml
?
Yes, this is possible, just add them to the document, you are using them. But I strongly discourage the use of entities, for including other document (parts)! As soon or later you will run in the difficulty, that one (or more) of the document (parts) are not available. Your document will not render and searching for the issue causing it is rather nasty. A far better solution is to use XInclude, for inclusion of documents.
Solution with ENTITY entries
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"
[
<!ENTITY section1 SYSTEM "../fragments/section1.xml">
<!ENTITY section2 SYSTEM "../fragments/section2.xml">
<!ENTITY section3 SYSTEM "../fragments/section3.xml">
]>
<book>
<chapter>
<title>Chapter 1</title>
§ion1;
§ion2;
§ion3;
</chapter>
</book>
And the other document file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"
[
<!ENTITY section3_a SYSTEM "../fragments/section3_a.xml">
<!ENTITY section3_b SYSTEM "../fragments/section3_b.xml">
<!ENTITY section3_c SYSTEM "../fragments/section3_c.xml">
]>
<section id="Section3">
<title>Section 3</title>
§ion3_a;
§ion3_b;
§ion3_c;
</section>
TIP: You can even move the entities all together out of your documents, see the answer I wrote on this question DocBook macros?
Solution with XInclude
Here then an example of how to set up documents with XInclude. Entity entries are used for small strings. And using XInclude, for file inclusion.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
<!ENTITY maven.project.version "(not set)">
<!ENTITY % entities SYSTEM "entities.ent" >
%entities;
]>
<book>
<title>&product.name;</title>
<subtitle>Release Notes</subtitle>
<bookinfo>
<date>&product.release.date;</date>
<releaseinfo><?eval ${project.version}?></releaseinfo>
</bookinfo>
<!-- Include chapters -->
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="changes/chapter-release-2.0.xml" />
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="changes/chapter-release-2.1.xml" />
</book>
A chapter file (put in the directory changes
), as which is included by the previous document. If xi:include
is used as above, it will stop rendering, if the href
attribute can not be solved.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"[
<!ENTITY section-changes "section-changes-v2.0.xml">
<!ENTITY % entities SYSTEM "../entities.ent">
%entities;
]>
<chapter id="release-2.0">
<title>Release 2.0</title>
<para>
Information given in this chapter applies for <emphasis>&product.name; v2.0</emphasis>.
</para>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="§ion-changes;">
<xi:fallback><para>FIXME File not found: "§ion-changes;"</para></xi:fallback>
</xi:include>
</chapter>
Here the xi:include
is used with a fallback, so if the href
attribute of the xi:include
could not be solved, the fallback is rendered into the document (This will show a paragraph with FIXME in it. Here using actually an entity, for referencing the document (location). This is great as that reference can then be used in the href
and FIXME section!
Be careful with referencing back to the entities file ../entities.ent
This again can give nasty errors when it can not be resolved.
这篇关于ENTITY声明可以嵌套在引用的XML文件中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!