使用名称空间访问XML属性 [英] Accessing XML attributes with namespaces

查看:88
本文介绍了使用名称空间访问XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用命名空间访问属性?我的XML数据格式为

How one can access attributes with namespaces? My XML data are in a form

val d = <z:Attachment rdf:about="#item_1"></z:Attachment>

但以下与该属性不匹配

(d \\ "Attachment" \ "@about").toString

如果我从属性名称中删除名称空间组件,那么它将起作用.

If I remove the namespace component from the attribute's name then it works.

val d = <z:Attachment about="#item_1"></z:Attachment>
(d \\ "Attachment" \ "@about").toString

有什么想法如何在Scala中使用命名空间访问属性?

Any idea how to access attributes with namespaces in Scala?

推荐答案

API文档引用了以下语法ns \ "@{uri}foo".

The API documentation refers to the following syntax ns \ "@{uri}foo".

在您的示例中,未定义名称空间,Scala似乎将您的属性视为未前缀.参见d.attributes.getClass.

In your example there is no namespace defined, and it seems Scala considers your attribute as unprefixed. See d.attributes.getClass.

现在,如果您这样做:

val d = <z:Attachment xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" rdf:about="#item_1"></z:Attachment>

然后:

scala> d \ "@{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"
res21: scala.xml.NodeSeq = #item_1

scala> d.attributes.getClass
res22: java.lang.Class[_] = class scala.xml.PrefixedAttribute

这篇关于使用名称空间访问XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆