我XML::LibXML在Perl 工作.
假设我有两个$element由不同(不透明)XPath查询获得的引用.
(如何)如果两个$element(节点)引用是文档树中的相同元素,我可以确定吗?
$el1 == $el2就我所知,比较并不总是奏效.
我看到了一些奇怪的行为XML::LibXML.
以下代码旨在添加<year>2005</year>到两个<book>节点.这里有问题吗?我试过更改XPath查询(//library/book)但结果是一样的.
use strict;
use warnings;
use XML::LibXML;
my $xml = XML::LibXML->new->parse_string( << 'MAIN' );
<library>
<book>
<title>Perl Best Practices</title>
<author>Damian Conway</author>
<isbn>0596001738</isbn>
<pages>542</pages>
<image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif"
width="145" height="190" />
</book>
<book>
<title>Perl Cookbook, Second Edition</title>
<author>Tom Christiansen</author>
<author>Nathan Torkington</author>
<isbn>0596003137</isbn>
<pages>964</pages>
<image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif"
width="145" height="190" />
</book>
</library>
MAIN
my ( $age ) = XML::LibXML->new
->parse_string( '<year>2005</year>' )
->findnodes( './year' );
my @books = $xml->findnodes( '//book' );
$_->addChild( $age ) for @books; …Run Code Online (Sandbox Code Playgroud) 这是一个基本的 XML 文档示例
<book_reviewers>
<results>
<reviewer>
<name>Anne</name>
<profession>Catfish wrangler</profession>
</reviewer>
<reviewer>
<name>Bob</name>
<profession>Beer taster</profession>
</reviewer>
<reviewer>
<name>Charlie</name>
<profession>Gardener</profession>
</reviewer>
</results>
</book_reviewers>
Run Code Online (Sandbox Code Playgroud)
我想补充一点:
<reviewer>
<name>Joan</name>
<profession>Jett</profession>
</reviewer>
Run Code Online (Sandbox Code Playgroud)
我尝试了多种解决方案的组合,这是一个至少不会引发错误的解决方案,但是它也不起作用。
#!/usr/bin/perl
use XML::LibXML;
use strict;
my $filename = "cr.xml";
my $parser = XML::LibXML->new();
my $critic_details = $parser->parse_file("$filename") or die;
my $new_reviewer = $critic_details->documentElement;
my $reviewer_name = $critic_details->documentElement;
my $reviewer_prof = $critic_details->documentElement;
my $newnode = $critic_details->documentElement;
for my $reviewers($critic_details->findnodes("book_reviewers/results/reviewers")){
$new_reviewer = $reviewers->createElement("reviewer");
$reviewer_name = $new_reviewer->addChild("name");
$reviewer_name->appendText("Joan");
$reviewer_prof = $new_reviewer->addChild("profession");
$reviewer_prof->appendText("Jett");
$newnode = $reviewers->addSibling($new_reviewer); #also …Run Code Online (Sandbox Code Playgroud) 我XML::LibXML用来解析带有名称空间的XML文档。因此XML::LibXML::XPathContext,我习惯于使用findnodesXPath //u:model。这将正确返回3个节点。
我现在想findvalue在3个返回的XML::LibXML::Element对象上使用,但是无法确定有效的方法/ xpath。或者,我迭代子项并直接与nodeName匹配,但这不理想:
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::XPathContext;
my $dom = XML::LibXML->load_xml( IO => \*DATA );
my $context = XML::LibXML::XPathContext->new( $dom->documentElement() );
$context->registerNs( 'u' => 'http://www.ca.com/spectrum/restful/schema/response' );
for my $node ( $context->findnodes('//u:model') ) {
#my $mh = $node->findvalue('mh');
my ($mh)
= map { $_->textContent() }
grep { $_->nodeName() eq 'mh' } $node->childNodes();
#my $attr = $node->findvalue('attribute');
my ($attr)
= map { $_->textContent() }
grep { …Run Code Online (Sandbox Code Playgroud) 给出以下XML片段:
<outline>
<node1 attribute1="value1" attribute2="value2">
text1
</node1>
</outline>
Run Code Online (Sandbox Code Playgroud)
我如何获得此输出?
outline
node1=text1
node1 attribute1=value1
node1 attribute2=value2
Run Code Online (Sandbox Code Playgroud)
我已经研究过use XML::LibXML::Reader;,但该模块似乎只提供对其名称引用的属性值的访问.我如何首先获得属性名称列表?
我使用此代码创建具有预期输出的新节点:
<item desc="desc foobar"><![CDATA[qux]]></item>
Run Code Online (Sandbox Code Playgroud)
代码 :
open my $fh, "<", $xml_file;
binmode $fh;
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml(IO => $fh);
# create a new node in XML file
my $root = $doc->getDocumentElement();
my $new_element = $doc->createElement("item");
# FIXME
$new_element->appendTextNode(sprintf '<![CDATA[%s]]>', join "\n", @input);
$new_element->setAttribute('desc', $desc);
$root->appendChild($new_element);
close $fh;
open my $out, '>', $xml_file;
binmode $out;
$doc->toFH($out);
close $out;
Run Code Online (Sandbox Code Playgroud)
它可以很好地创建新的元素文本,但我想知道如何在没有XML实体替换的情况下添加CDATA:我得到:
<item desc="dddd"><![CDATA[qux]]>
# ^^^^
Run Code Online (Sandbox Code Playgroud) 这个xpath是一个有效的XPath表达式吗?(它做它应该做的).
#!/usr/bin/env perl
use strict; use warnings; use 5.012;
use XML::LibXML;
my $string =<<EOS;
<result>
<cd>
<artists>
<artist class="1">Pumkinsingers</artist>
<artist class="2">Max and Moritz</artist>
</artists>
<title>Hello, Hello</title>
</cd>
<cd>
<artists>
<artist class="3">Green Trees</artist>
<artist class="4">The Leons</artist>
</artists>
<title>The Shield</title>
</cd>
</result>
EOS
#/
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml( string => $string );
my $root = $doc->documentElement;
my $xpath = '/result/cd[artists[artist[@class="2"]]]/title';
my @nodes = $root->findnodes( $xpath );
for my $node ( @nodes ) {
say $node->textContent;
}
Run Code Online (Sandbox Code Playgroud) 我已经下载了草莓PERL并在Winxp sp3上用CGI Perl Apache编写了一个应用程序.我使用的其中一个库(由其他人编写)使用XML :: LibXML.当我加载页面时,它给出了内部服务器错误.从Apache错误日志中我可以看到此错误:
无法为模块XML :: LibXML加载'C:/strawberry/perl/site/lib/auto/XML/LibXML/LibXML.dll':load_file:指定的模块无法可以在C:/strawberry/perl/lib/DynaLoader.pm第190行找到
.C:/strawberry/perl/site/lib/auto/XML/LibXML/LibXML.dll存在所有权限.此库也适用于Linux.如果我删除所有需要LibXML的代码,我的应用程序也可以正常工作.
任何人都可以告诉我何时可以在这里发布问题.
我正在将 XML 文档转换为 HTML。需要做的事情之一是删除命名空间,命名空间不能在 HTML 中合法声明(除非它是根标记中的 XHTML 命名空间)。我发现过 5 到 10 年前的帖子,介绍使用 XML::LibXML 和 LibXML2 来实现这一点有多么困难,但最近没有那么多。这是一个例子:
use XML::LibXML;
use XML::LibXML::XPathContext;
use feature 'say';
my $xml = <<'__EOI__';
<myDoc>
<par xmlns:bar="www.bar.com">
<bar:foo/>
</par>
</myDoc>
__EOI__
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($xml);
my $bar_foo = do{
my $xpc = XML::LibXML::XPathContext->new($doc);
$xpc->registerNs('bar', 'www.bar.com');
${ $xpc->findnodes('//bar:foo') }[0];
};
$bar_foo->setNodeName('foo');
$bar_foo->setNamespace('','');
say $bar_foo->nodeName; #prints 'bar:foo'. Dang!
my @namespaces = $doc->findnodes('//namespace::*');
for my $ns (@namespaces){
# $ns->delete; #can't find any such method for namespaces …Run Code Online (Sandbox Code Playgroud) 尝试按如下方式安装 XML 包时出现错误configure: error: "libxml not found"。看来 R 正在从 Anaconda 获取 libxml2 安装。我怎样才能解决这个问题?
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"\nCopyright (C) 2018 The R Foundation for Statistical Computing\nPlatform: x86_64-pc-linux-gnu (64-bit)\n\n> install.packages("XML")\nInstalling package into \xe2\x80\x98/home/bravegag/R/x86_64-pc-linux-gnu-library/3.4\xe2\x80\x99\n(as \xe2\x80\x98lib\xe2\x80\x99 is unspecified)\ntrying URL \'https://cloud.r-project.org/src/contrib/XML_3.98-1.19.tar.gz\'\nContent type \'application/x-gzip\' length 1600788 bytes (1.5 MB)\n==================================================\ndownloaded 1.5 MB\n\n* installing *source* package \xe2\x80\x98XML\xe2\x80\x99 ...\n** package \xe2\x80\x98XML\xe2\x80\x99 successfully unpacked and MD5 sums checked\nchecking for gcc... gcc\nchecking whether the C compiler works... yes\nchecking for C compiler default output …Run Code Online (Sandbox Code Playgroud) xml-libxml ×10
perl ×8
xml ×6
libxml2 ×2
attr ×1
cgi-bin ×1
perl-module ×1
r ×1
ubuntu-14.04 ×1
xpath ×1