XPath/getChildElements 都无法在 XOM 中获取 XML 子元素

发布于 2024-11-29 08:03:27 字数 1924 浏览 2 评论 0原文

我必须解析 OAI-PMH XML 文件,如下所示。我想迭代 ListRecord 中的所有 节点。

<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <responseDate>2010-12-30T10:46:39.654+08:00</responseDate>
  <request verb="ListRecords" metadataPrefix="oai_dc">http://172.16.1.118/ahd/oai2.do</request>
  <ListRecords>
    <record>
      <header>
        <identifier>9010402101001001</identifier>
      </header>
      <metadata>
        <oai_dc:dc xsi:schemaLocationfiltered="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">
          <dc:identifier>9010402101001001</dc:identifier>
        </oai_dc:dc>
      </metadata>
    </record>
    <resumptionToken>1509/1509</resumptionToken>
  </ListRecords>
</OAI-PMH>

但是当我使用 XOM 1.2.5 获取这些节点时,无论我使用什么方法(查询或 getChildElements)它总是返回 0 个节点。

以下是我在 Scala 解释器中使用的代码:

scala> import nu.xom.Builder
import nu.xom.Builder

scala> val builder = new Builder
builder: nu.xom.Builder = nu.xom.Builder@6682d439

scala> val document = builder.build(new java.io.File("/home/brianhsu/qqq.xml"))
document: nu.xom.Document = [nu.xom.Document: OAI-PMH]

scala> document.query("//record").size
res0: Int = 0

scala> document.query("//ListRecords").size
res1: Int = 0

scala> document.getRootElement.getChildElements("ListRecords").size
res2: Int = 0

我不知道为什么无法在 XML 中获取 ListRecordsrecord 。我错过了什么吗?

I've to parse an OAI-PMH XML file, which looks like the following. I would like to iterate over all <record> nodes in ListRecord.

<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <responseDate>2010-12-30T10:46:39.654+08:00</responseDate>
  <request verb="ListRecords" metadataPrefix="oai_dc">http://172.16.1.118/ahd/oai2.do</request>
  <ListRecords>
    <record>
      <header>
        <identifier>9010402101001001</identifier>
      </header>
      <metadata>
        <oai_dc:dc xsi:schemaLocationfiltered="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">
          <dc:identifier>9010402101001001</dc:identifier>
        </oai_dc:dc>
      </metadata>
    </record>
    <resumptionToken>1509/1509</resumptionToken>
  </ListRecords>
</OAI-PMH>

But when I using XOM 1.2.5 to get those node, no matter what method I use (query or getChildElements) it always return 0 nodes.

The following is the code I use in Scala interpreter:

scala> import nu.xom.Builder
import nu.xom.Builder

scala> val builder = new Builder
builder: nu.xom.Builder = nu.xom.Builder@6682d439

scala> val document = builder.build(new java.io.File("/home/brianhsu/qqq.xml"))
document: nu.xom.Document = [nu.xom.Document: OAI-PMH]

scala> document.query("//record").size
res0: Int = 0

scala> document.query("//ListRecords").size
res1: Int = 0

scala> document.getRootElement.getChildElements("ListRecords").size
res2: Int = 0

I've no idea why I could not get ListRecords and record in the XML. Did I miss something?

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

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

发布评论

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

评论(2

執念 2024-12-06 08:03:27

我发现这是 XPath 表达式返回任何内容的重复项//element, but //* returns a count

下面的代码有效,我需要将标签名称绑定到命名空间。

scala> val context = new XPathContext("xsi", "http://www.openarchives.org/OAI/2.0/")
context: nu.xom.XPathContext = nu.xom.XPathContext@19a3f495

scala> document.query("//xsi:record", context).size
res6: Int = 1

I found this is a duplicate of XPath Expression returns nothing for //element, but //* returns a count

The following code works, I need to bind the tag name to a namespace.

scala> val context = new XPathContext("xsi", "http://www.openarchives.org/OAI/2.0/")
context: nu.xom.XPathContext = nu.xom.XPathContext@19a3f495

scala> document.query("//xsi:record", context).size
res6: Int = 1
眼角的笑意。 2024-12-06 08:03:27

我敢打赌这是一个 xmlns 问题 - 您是否尝试过使用域参数?尝试:

 document.getRootElement
         .getChildElements("ListRecords", 
                           "http://www.openarchives.org/OAI/2.0/").size

基本上,许多语言在为 XML 对象指定默认 ns 时,将要求该命名空间查找该节点——即使它在输出的 DOM 本身中没有前缀。

(这也可以使用 XPathContext 对象来完成,如 Brian Hsu 所示)

I'll wager that it is a xmlns issue -- have you tried using the domain parameter? Try:

 document.getRootElement
         .getChildElements("ListRecords", 
                           "http://www.openarchives.org/OAI/2.0/").size

Basically, many languages, when given a default ns on an XML object, will require that namespace to look that node up -- even if it is not prefixed in the outputted DOM itself.

(This can also be done using the XPathContext object, as illustrated by Brian Hsu)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文