Perl、LibXML 和模式
我有一个示例 Perl 脚本,我试图根据模式加载和验证文件,它们询问各个节点。
#!/usr/bin/env perl
use strict;
use warnings;
use XML::LibXML;
my $filename = 'source.xml';
my $xml_schema = XML::LibXML::Schema->new(location=>'library.xsd');
my $parser = XML::LibXML->new ();
my $doc = $parser->parse_file ($filename);
eval {
$xml_schema->validate ($doc);
};
if ($@) {
print "File failed validation: $@" if $@;
}
eval {
print "Here\n";
foreach my $book ($doc->findnodes('/library/book')) {
my $title = $book->findnodes('./title');
print $title->to_literal(), "\n";
}
};
if ($@) {
print "Problem parsing data : $@\n";
}
不幸的是,虽然它可以很好地验证 XML 文件,但它没有找到任何 $book 项目,因此没有打印出任何内容。
如果我从 XML 文件中删除架构并从 PL 文件中删除验证,那么它就可以正常工作。
我正在使用默认名称空间。 如果我将其更改为不使用默认命名空间 (xmlns:lib="http://libs.domain.com" 并使用 lib 为 XML 文件中的所有项目添加前缀,并将 XPath 表达式更改为包含命名空间前缀 (/lib: Library/lib:book) 然后它又可以工作了,
为什么?
XML:
<?xml version="1.0" encoding="utf-8"?>
<library xmlns="http://lib.domain.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://lib.domain.com .\library.xsd">
<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>
<book>
<title>Guitar for Dummies</title>
<author>Mark Phillips</author>
<author>John Chappell</author>
<isbn>076455106X</isbn>
<pages>392</pages>
<image src="http://media.wiley.com/product_data/coverImage/6X/07645510/076455106X.jpg" width="100" height="125"/>
</book>
</library>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://lib.domain.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://lib.domain.com">
<xs:attributeGroup name="imagegroup">
<xs:attribute name="src" type="xs:string"/>
<xs:attribute name="width" type="xs:integer"/>
<xs:attribute name="height" type="xs:integer"/>
</xs:attributeGroup>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element maxOccurs="unbounded" name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
<xs:element name="pages" type="xs:integer"/>
<xs:element name="image">
<xs:complexType>
<xs:attributeGroup ref="imagegroup"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I have an example Perl script which I am trying to load and validate a file against a schema, them interrogate various nodes.
#!/usr/bin/env perl
use strict;
use warnings;
use XML::LibXML;
my $filename = 'source.xml';
my $xml_schema = XML::LibXML::Schema->new(location=>'library.xsd');
my $parser = XML::LibXML->new ();
my $doc = $parser->parse_file ($filename);
eval {
$xml_schema->validate ($doc);
};
if ($@) {
print "File failed validation: $@" if $@;
}
eval {
print "Here\n";
foreach my $book ($doc->findnodes('/library/book')) {
my $title = $book->findnodes('./title');
print $title->to_literal(), "\n";
}
};
if ($@) {
print "Problem parsing data : $@\n";
}
Unfortunately, although it is validating the XML file fine, it is not finding any $book items and therefore not printing out anything.
If I remove the schema from the XML file and the validation from the PL file then it works fine.
I am using the default namespace. If I change it to not use the default namespace (xmlns:lib="http://libs.domain.com" and prefix all items in the XML file with lib and change the XPath expressions to include the namespace prefix (/lib:library/lib:book) then it again works file.
Why? and what am I missing?
XML:
<?xml version="1.0" encoding="utf-8"?>
<library xmlns="http://lib.domain.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://lib.domain.com .\library.xsd">
<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>
<book>
<title>Guitar for Dummies</title>
<author>Mark Phillips</author>
<author>John Chappell</author>
<isbn>076455106X</isbn>
<pages>392</pages>
<image src="http://media.wiley.com/product_data/coverImage/6X/07645510/076455106X.jpg" width="100" height="125"/>
</book>
</library>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://lib.domain.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://lib.domain.com">
<xs:attributeGroup name="imagegroup">
<xs:attribute name="src" type="xs:string"/>
<xs:attribute name="width" type="xs:integer"/>
<xs:attribute name="height" type="xs:integer"/>
</xs:attributeGroup>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element maxOccurs="unbounded" name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
<xs:element name="pages" type="xs:integer"/>
<xs:element name="image">
<xs:complexType>
<xs:attributeGroup ref="imagegroup"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 XML::LibXML 文档:
因此,从 XPath 的角度来看,不存在“默认”命名空间……对于任何非空命名空间,您必须在 XPath 中指定它。 XML::LibXML::XPathContext 模块允许您为任何名称空间创建前缀以在 XPath 表达式中使用。
From the XML::LibXML docs:
So, from the perspective of XPath, there is no "default" namespace...for any non-null namespace, you have to specify it in your XPath. The XML::LibXML::XPathContext module lets you create a prefix for any namespace to use in your XPath expression.