2

I am trying to use some xpath expressions to extract info from an XML file that looks like this (it is an OAI-PMH protocol response):

<?xml version="1.0" encoding="UTF-8"?>

<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/
                             http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
  <responseDate>2013-10-11T09:24:55Z</responseDate>
  <request verb="ListRecords" metadataPrefix="oai_dc">http://request.url.com/oai</request>
  <ListRecords>
    <record>
      <header>
        <identifier>oai:identifier:item1</identifier>
        <datestamp>2012-06-07T12:03:53Z</datestamp>
        <setSpec>set:identifier</setSpec>
      </header>
      <metadata>
        <oai_dc:dc
            xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
                                http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title xml:lang="en-US">Title</dc:title>
          <dc:creator>creator</dc:creator>
          <dc:description xml:lang="en-US">abstract</dc:description>
          <dc:publisher xml:lang="en-US">publisher</dc:publisher>
          <dc:contributor xml:lang="en-US"></dc:contributor>
          <dc:date>2011-10-18</dc:date>
          <dc:type xml:lang="en-US"></dc:type>
          <dc:format>application/pdf</dc:format>
          <dc:identifier>identifier</dc:identifier>
          <dc:source xml:lang="en-US">source</dc:source>
        </oai_dc:dc>
      </metadata>
    </record>
    <record>
     ...
    </record>
     ...
  </ListRecords>
</OAI-PMH>

Ultimately I will have to write some Java code to do this, but I'd like to know more about the xpath and namespaces and for that reason I use the command line tool xqilla.

After some research (eg this) and many failed attempts I tried the following expression:

//*[local-name()='title']

but I get the following error:

:1:22: error: No namespace for prefix 'xs' [err:XPST0081]

Could someone point me to the right direction please? The documentation of xqilla has not proven very helpful so far.

Thanks.

Edit: Since the title is not exactly what is being asked here, a follow up (and more general) question would be how does one define namespaces in general using xqilla? Because if I try the expression:

//dc:title

the error I get is

/tmp/foo.xq:1:3: error: No namespace for prefix 'dc' [err:XPST0081]

I am running xqilla like this:

xqilla -p -i oai_response.xml foo.xq
4

1 回答 1

3

XQilla 可以执行 XPath,但默认情况下它使用 XQuery。

您可以像这样创建一个 xquery 文件,例如:my.file.xquery

declare namespace dc="http://purl.org/dc/elements/1.1/";
doc("my.file.xml")//dc:title

然后运行它

xqilla my.file.xquery

如果您只想使用 xpath,我不确定如何指定命名空间您可以做的是使用命名空间通配符。所以把它放在 my.file.xpath

//*:title

并运行它

xqilla -p -i my.file.xpath my.file.xml
于 2014-09-19T08:25:56.067 回答