首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LibXML和模式

LibXML和模式
EN

Stack Overflow用户
提问于 2009-03-18 12:31:22
回答 1查看 3.8K关注 0票数 2

我有一个示例Perl脚本,我试图根据模式加载和验证文件,它们询问不同的节点。

代码语言:javascript
复制
#!/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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-03-18 16:29:32

XML::LibXML docs

关于XPath的一个常见错误是假设节点测试由没有前缀的元素名组成,与默认名称空间中的元素相匹配。这个假设是错误的--根据XPath规范,这样的节点测试只能匹配无(即空)名称空间中的元素。...(以及以后)......The推荐的方式是使用XML::LibXML::XPathContext模块

因此,从XPath的角度来看,没有任何非空名称空间的“默认”namespace...for,您必须在XPath中指定它。XPathContext模块允许您为要在XPath表达式中使用的任何命名空间创建前缀。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/658060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档