xml - 斯卡拉 XML API : Why allow NodeSeq as attribute values?

标签 xml scala

属性值似乎是 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 类型?

最佳答案

来自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 中尝试过了,但它没有完全编译 - 我需要使用 new Atom(42)。但我可以输入如下内容:

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

因此,这是利用节点获取属性的部分理由。是的,这有点时髦。

关于xml - 斯卡拉 XML API : Why allow NodeSeq as attribute values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4622218/

相关文章:

scala - Scala元组的常规 'map'函数?

scala - Scala 中的正则表达式和模式匹配第二部分

c# - 如果返回格式是 xml,如何删除 web api 中的模式节点?

android 自动生成带.out 的xml 文件

scala - 如何禁用 java ortools CP 求解器日志记录?

scala - 给定关键字在 Scala 3 或 dotty 中如何工作?

scala - 省略括号

java - 循环动画可绘制

php - 使用php解析XML数据放入mysql数据库

xml - Saxon 过早评估 xsl :attribute-set