如何使用 perl/LibXML 在 DOCTYPE 中创建 ENTITY 引用
How do I create ENTITY references in the DOCTYPE using perl/LibXML
我正在尝试创建以下包含实体声明的 DTD:
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd"
[ <!ENTITY icon.url "https://example.com/icon.png">
<!ENTITY base.url "https://example.com/content/" > ]>
我可以成功创建 DOCTYPE 而无需 实体引用:
#!/usr/bin/perl -w
use strict;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0','UTF-8');
my $dtd = $doc->createInternalSubset( "LinkSet", "-//NLM//DTD LinkOut 1.0//EN", "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd" );
my $ls = $doc->createElement( "LinkSet" );
$doc->setDocumentElement($ls);
print $doc->toString;
exit;
结果:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd">
<LinkSet/>
XML::LibXML documentation 展示了如何向文档添加实体引用,但没有展示如何在 DOCTYPE 中声明实体。
A 指向将 ENTITY 引用创建为字符串并对其进行解析。
这也是 Perl 中最好的方法吗?
XML::LibXML::Document
的文档
说这个
[The Document Class] inherits all functions from XML::LibXML::Node
as specified in the DOM
specification. This enables access to the nodes besides the root element
on document level - a "DTD" for example. The support for these nodes is
limited at the moment.
后面也明确指出,这些限制的来源是libxml2
本身,而不是Perl模块。这是有道理的,因为 DTD 具有与 XML(甚至 XML 处理指令)完全不同的语法,即使它表面上看起来相似。
唯一的方法似乎是使用所需的 DTD 解析基本文档并使用它
像这样
use strict;
use warnings 'all';
use XML::LibXML;
my $doc = XML::LibXML->load_xml(string => <<__END_XML__);
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd"
[
<!ENTITY icon.url "https://example.com/icon.png">
<!ENTITY base.url "https://example.com/content/">
]>
<LinkSet/>
__END_XML__
print $doc;
输出
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd" [
<!ENTITY icon.url "https://example.com/icon.png">
<!ENTITY base.url "https://example.com/content/">
]>
<LinkSet/>
我正在尝试创建以下包含实体声明的 DTD:
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd"
[ <!ENTITY icon.url "https://example.com/icon.png">
<!ENTITY base.url "https://example.com/content/" > ]>
我可以成功创建 DOCTYPE 而无需 实体引用:
#!/usr/bin/perl -w
use strict;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0','UTF-8');
my $dtd = $doc->createInternalSubset( "LinkSet", "-//NLM//DTD LinkOut 1.0//EN", "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd" );
my $ls = $doc->createElement( "LinkSet" );
$doc->setDocumentElement($ls);
print $doc->toString;
exit;
结果:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd">
<LinkSet/>
XML::LibXML documentation 展示了如何向文档添加实体引用,但没有展示如何在 DOCTYPE 中声明实体。
A
XML::LibXML::Document
的文档
说这个
[The Document Class] inherits all functions from
XML::LibXML::Node
as specified in the DOM specification. This enables access to the nodes besides the root element on document level - a "DTD" for example. The support for these nodes is limited at the moment.
后面也明确指出,这些限制的来源是libxml2
本身,而不是Perl模块。这是有道理的,因为 DTD 具有与 XML(甚至 XML 处理指令)完全不同的语法,即使它表面上看起来相似。
唯一的方法似乎是使用所需的 DTD 解析基本文档并使用它
像这样
use strict;
use warnings 'all';
use XML::LibXML;
my $doc = XML::LibXML->load_xml(string => <<__END_XML__);
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd"
[
<!ENTITY icon.url "https://example.com/icon.png">
<!ENTITY base.url "https://example.com/content/">
]>
<LinkSet/>
__END_XML__
print $doc;
输出
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE LinkSet PUBLIC "-//NLM//DTD LinkOut 1.0//EN" "https://www.ncbi.nlm.nih.gov/projects/linkout/doc/LinkOut.dtd" [
<!ENTITY icon.url "https://example.com/icon.png">
<!ENTITY base.url "https://example.com/content/">
]>
<LinkSet/>