gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:13 26 4
gpt4 key购买 nike

属性值似乎是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com