文档顺序是defined
有一个排序,文档顺序,定义在文档的所有节点上,对应于每个节点的XML表示的第一个字符在文档的XML表示中出现的顺序一般实体扩充后。因此,根节点将是第一个节点。元素节点出现在它们的子节点之前。因此,文档顺序按元素节点在 XML 中出现的顺序对元素节点进行排序(在实体展开之后)。
换句话说,就是事物在 XML 文档中发生的顺序。 XML::XPath 模块按文档顺序生成结果。例如:
#! /usr/bin/perl
use warnings;
use strict;
use XML::XPath;
my $entity_template = "/Entities"
. "/Entity"
. "[EntityName='!!NAME!!']"
;
my $tables_path = join "|" =>
qw( ./Tables/DataTables/DataTable
./Tables/OtherTables/OtherTable );
my $xp = XML::XPath->new(ioref => *DATA);
foreach my $ename (qw/ foo bar /) {
print "$ename:\n";
(my $path = $entity_template) =~ s/!!NAME!!/$ename/g;
foreach my $n ($xp->findnodes($path)) {
foreach my $t ($xp->findnodes($tables_path, $n)) {
print $t->toString, "\n";
}
}
}
__DATA__
第一个表达式搜索<Entity> 元素,其中每个元素都有一个<ElementName> 子元素,其string-value 是选定的实体名称。从那里,我们寻找<DataTable> 或<OtherTable>。
给定输入
<Entities>
<Entity>
<EntityName>foo</EntityName>
<EntityType>type1</EntityType>
<Tables>
<DataTables>
<DataTable>1</DataTable>
<DataTable>2</DataTable>
</DataTables>
<OtherTables>
<OtherTable>3</OtherTable>
<OtherTable>4</OtherTable>
</OtherTables>
</Tables>
</Entity>
<Entity>
<EntityName>bar</EntityName>
<EntityType>type2</EntityType>
<Tables>
<DataTables>
<DataTable>5</DataTable>
<DataTable>6</DataTable>
</DataTables>
<OtherTables>
<OtherTable>7</OtherTable>
<OtherTable>8</OtherTable>
</OtherTables>
</Tables>
</Entity>
</Entities>
输出是
foo:
<DataTable>1</DataTable>
<DataTable>2</DataTable>
<OtherTable>3</OtherTable>
<OtherTable>4</OtherTable>
bar:
<DataTable>5</DataTable>
<DataTable>6</DataTable>
<OtherTable>7</OtherTable>
<OtherTable>8</OtherTable>
要提取字符串值(“内部文本”),请将$tables_path 更改为
my $tables_path = ". / Tables / DataTables / DataTable / text() |
. / Tables / OtherTables / OtherTable / text()";
是的,这是重复的——因为 XML::XPath 实现了XPath 1.0。
输出:
富:
1
2
3
4
酒吧:
5
6
7
8