XPath/getChildElements 都无法在 XOM 中获取 XML 子元素
我必须解析 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 中获取 ListRecords
和 record
。我错过了什么吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现这是 XPath 表达式返回任何内容的重复项//element, but //* returns a count
下面的代码有效,我需要将标签名称绑定到命名空间。
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.
我敢打赌这是一个
xmlns
问题 - 您是否尝试过使用域参数?尝试:基本上,许多语言在为 XML 对象指定默认 ns 时,将要求该命名空间查找该节点——即使它在输出的 DOM 本身中没有前缀。
(这也可以使用 XPathContext 对象来完成,如 Brian Hsu 所示)
I'll wager that it is a
xmlns
issue -- have you tried using the domain parameter? Try: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)