加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala XML API:为什么允许NodeSeq作为属性值?

发布时间:2020-12-16 19:16:28 所属栏目:安全 来源:网络整理
导读:似乎属性值是Seq [Node]类型. scala a b="1"/.attribute("b") res11: Option[Seq[scala.xml.Node]] = Some(1) 这意味着您可以将XML指定为属性值. scala a b={zx//z}/.attribute("b") res16: Option[Seq[scala.xml.Node]] = Some(zx/x/z)scala a b={zx//z}/.a
似乎属性值是Seq [Node]类型.

scala> <a b="1"/>.attribute("b")             
res11: Option[Seq[scala.xml.Node]] = Some(1)

这意味着您可以将XML指定为属性值.

scala> <a b={<z><x/></z>}/>.attribute("b")            
res16: Option[Seq[scala.xml.Node]] = Some(<z><x></x></z>)

scala> <a b={<z><x/></z>}/>.attribute("b").map(_  "x")
res17: Option[scala.xml.NodeSeq] = Some(<x></x>)

scala> new xml.PrettyPrinter(120,2).format(<a b={<z><x/></z>}/>)
res19: String = <a b="<z><x></x></z>"></a>

这对我来说似乎很时髦.我从未在现实世界中看到XML作为属性值.为什么允许?为什么属性值不是String类型?

解决方法

来自Burak Emir的 scala.xml “draft” book:

开始报价

At first sight,it appears that attributes should only be strings and nothing else. However,there are two reasons to allow the same kind of nodes (other than element nodes) that can appear within XML: data values and entity references.

<foo name= "s&uuml;ss" life={Atom(42)}>

结束语

现在我已经在2.8.0中尝试了它并且它没有完全编译 – 我需要使用新的Atom(42).但我可以输入这样的东西:

<foo name={List(Text("s"),EntityRef("uuml"),Text("ss"))}/>

这是利用节点属性的基本原理的一部分.是的它有点时髦.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读