gpt4 book ai didi

perl - 为什么这个 xmlns 属性会打乱我的 xpath 查询?

转载 作者:行者123 更新时间:2023-12-03 16:58:00 24 4
gpt4 key购买 nike

我正在使用 LibXML 解析一个简单的 jhove 输出。但是,我没有得到我期望的值。这是代码:

use feature "say";
use XML::LibXML;

my $PRSR = XML::LibXML->new();
my $xs=<DATA>;
say $xs;
my $t1 = $PRSR->load_xml(string => $xs);
say "1:" . $t1->findvalue('//date');
$xs=<DATA>;
say $xs;
$t1 = $PRSR->load_xml(string => $xs);
say "2:" . $t1->findvalue('//date');


__DATA__
<jhove xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://hul.harvard.edu/ois/xml/ns/jhove" xsi:schemaLocation="http://hul.harvard.edu/ois/xml/ns/jhove http://hul.harvard.edu/ois/xml/xsd/jhove/1.3/jhove.xsd" name="Jhove" release="1.0 (beta 3)" date="2005-02-04"><date>2006-10-06T09:11:34+02:00</date></jhove>
<jhove><date>2006-10-06T09:11:34+02:00</date></jhove>

如您所见,“1:”行返回一个空字符串,而“2:”行返回预期日期。阻止 xpath 查询正常工作的 jhove-root-element 中有什么?我什至在 XML-Spy 中尝试过,即使有完整的 header ,它也能正常工作。

编辑:当我从根元素中删除 xmlns 属性时,xpath 查询有效。但这怎么可能呢?

最佳答案

XML::LibXML::Node文档特别提到了这个问题以及如何处理它......

NOTE ON NAMESPACES AND XPATH:

A common mistake about XPath is to assume that node tests consisting of an element name with no prefix match elements in the default namespace. This assumption is wrong - by XPath specification, such node tests can only match elements that are in no (i.e. null) namespace.

So, for example, one cannot match the root element of an XHTML document with $node->find('/html') since '/html' would only match if the root element <html> had no namespace, but all XHTML elements belong to the namespace http://www.w3.org/1999/xhtml. (Note that xmlns="..." namespace declarations can also be specified in a DTD, which makes the situation even worse, since the XML document looks as if there was no default namespace).

There are several possible ways to deal with namespaces in XPath:

  • The recommended way is to use the XML::LibXML::XPathContext module to define an explicit context for XPath evaluation, in which a document independent prefix-to-namespace mapping can be defined. For example:

    my $xpc = XML::LibXML::XPathContext->new;
    $xpc->registerNs('x', 'http://www.w3.org/1999/xhtml');
    $xpc->find('/x:html',$node);
  • Another possibility is to use prefixes declared in the queried document (if known). If the document declares a prefix for the namespace in question (and the context node is in the scope of the declaration), XML::LibXML allows you to use the prefix in the XPath expression, e.g.:

    $node->find('/x:html');

关于perl - 为什么这个 xmlns 属性会打乱我的 xpath 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com