如何使用 XPath 查询 eXist?

发布于 2024-11-09 08:41:56 字数 2151 浏览 0 评论 0原文

我决定使用 eXist 作为我用 Perl 编写的应用程序的数据库, 我正在尝试它。问题是我存储了具有以下结构的 .xml 文档

<foo-bar00>
    <perfdata datum="GigabitEthernet3_0_18">
        <cli cmd="whatsup" detail="GigabitEthernet3/0/18" find="" given="">
            <input_rate>3</input_rate>
            <output_rate>3</output_rate>
        </cli>
    </perfdata>
    <timeline>2011-5-23T11:15:33</timeline>
</foo-bar00>

,它位于“/db/LAB/foo-bar00/2011/5/23/11_15_33.xml”集合中。

我可以成功查询它,例如

my $xquery = 'doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")' ;

$xquery 可以等于 = doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/perfdata/cli/data(output_rate) 或者 = doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/data(timeline)

my ($rc1, $set) = $eXist->executeQuery($xquery) ;
my ($rc2, $count) = $eXist->numberOfResults($set) ;
my ($rc3, @data) = $eXist->retrieveResults($set) ;
$eXist->releaseResultSet($set) ;
print Dumper(@data) ;

结果是:

$VAR1 = {
  'hitCount' => 1,
  'foo-bar00' => {
    'perfdata' => {
      'cli' => {
        'given' => '',
        'detail' => 'GigabitEthernet3/0/18',
        'input_rate' => '3',
        'cmd' => 'whatsup',
        'output_rate' => '3',
        'find' => ''
      },
      'datum' => 'GigabitEthernet3_0_18'
    },
    'timeline' => '2011-5-23T11:15:33'
  }
};

--->鉴于我知道我想要从中检索信息的 xml 文档。 --->鉴于我想检索时间线信息。

当我写作时:

my $db_xml_doc = "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" ;
my ($db_rc, $db_datum) = $eXist->queryXPath("/foo-bar00/timeline", $db_xml_doc, "") ;
print Dumper($db_datum) ;

结果是:

$VAR1 = {
  'hash' => 1717362942,
  'id' => 3,
  'results' => [
    {
      'node_id' => '1.2',
      'document' => '/db/LAB/foo-bar00/2011/5/23/11_15_33.xml'
    }
  ]
};

问题是:如何检索“时间线”信息?似乎“node_id”变量(=1.2)可以指向“时间线”信息,但我该如何使用它?

谢谢。

I decided to use eXist as a database for an application that I am writing in Perl and
I am experimenting with it. The problem is that I have stored a .xml document with the following structure

<foo-bar00>
    <perfdata datum="GigabitEthernet3_0_18">
        <cli cmd="whatsup" detail="GigabitEthernet3/0/18" find="" given="">
            <input_rate>3</input_rate>
            <output_rate>3</output_rate>
        </cli>
    </perfdata>
    <timeline>2011-5-23T11:15:33</timeline>
</foo-bar00>

and it is located in the "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" collection.

I can successfully query it, like

my $xquery = 'doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")' ;

or $xquery can be equal to
= doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/perfdata/cli/data(output_rate)
or
= doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/data(timeline)

my ($rc1, $set) = $eXist->executeQuery($xquery) ;
my ($rc2, $count) = $eXist->numberOfResults($set) ;
my ($rc3, @data) = $eXist->retrieveResults($set) ;
$eXist->releaseResultSet($set) ;
print Dumper(@data) ;

And the result is :

$VAR1 = {
  'hitCount' => 1,
  'foo-bar00' => {
    'perfdata' => {
      'cli' => {
        'given' => '',
        'detail' => 'GigabitEthernet3/0/18',
        'input_rate' => '3',
        'cmd' => 'whatsup',
        'output_rate' => '3',
        'find' => ''
      },
      'datum' => 'GigabitEthernet3_0_18'
    },
    'timeline' => '2011-5-23T11:15:33'
  }
};

---> Given that I know the xml document that I want to retrieve info from.
---> Given that I want to retrieve the timeline information.

When I am writing :

my $db_xml_doc = "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" ;
my ($db_rc, $db_datum) = $eXist->queryXPath("/foo-bar00/timeline", $db_xml_doc, "") ;
print Dumper($db_datum) ;

The result is :

$VAR1 = {
  'hash' => 1717362942,
  'id' => 3,
  'results' => [
    {
      'node_id' => '1.2',
      'document' => '/db/LAB/foo-bar00/2011/5/23/11_15_33.xml'
    }
  ]
};

The question is : How can I retrieve the "timeline" info ? Seems that the "node_id" variable (=1.2) can points to the "timeline" info, but how can I use it ?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空袭的梦i 2024-11-16 08:41:56
use XML::LibXML qw( );

my $parser = XML::LibXML->new();
my $doc    = $parser->parse_file('a.xml');
my $root   = $doc->documentElement();

my ($timeline) = $root->findnodes('timeline');
if ($timeline) {
    print("Exists: ", $timeline->textContent(), "\n");
}

或者

my ($timeline) = $root->findnodes('timeline/text()');
if ($timeline) {
    print("Exists: ", $timeline->getValue(), "\n");
}

我可以使用 /foo-bar00/timeline 而不是 timeline,但我认为没有必要。

use XML::LibXML qw( );

my $parser = XML::LibXML->new();
my $doc    = $parser->parse_file('a.xml');
my $root   = $doc->documentElement();

my ($timeline) = $root->findnodes('timeline');
if ($timeline) {
    print("Exists: ", $timeline->textContent(), "\n");
}

or

my ($timeline) = $root->findnodes('timeline/text()');
if ($timeline) {
    print("Exists: ", $timeline->getValue(), "\n");
}

I could have used /foo-bar00/timeline instead of timeline, but I didn't see the need.

所有深爱都是秘密 2024-11-16 08:41:56

不知道您是否仍然感兴趣,但您可以将文档作为 DOM 检索并对 DOM 应用 xquery,或者更好的是,仅在提交到服务器的查询中提取所需的信息。

像这样的东西:

for $p in doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")//output_rate
return
    <vlaue>$p</value>

Don't know if you're still interested, but you could either retrieve the doc as DOM and apply an xquery to the DOM, or, probably better, only pull out the info you want in the query that you submit to the server.

Something like this:

for $p in doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")//output_rate
return
    <vlaue>$p</value>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文