问题描述
在这个问题中,提问者想要这样转换文档:
In this question, the asker wants to transform documents like this:
<text>
The capitals of Bolivia are <blank/> and <blank/>.
</text>
进入这个:
<text>
The capitals of Bolivia are <input name="blank.1"> and <input name="blank.2">.
</text>
正如我在我的回答中所指出的,Anti-XML 的zippers 提供了一个这个问题的干净解决方案.例如,以下内容适用于重命名空白元素:
As I noted in my answer there, Anti-XML's zippers provide a clean solution to this problem. The following, for example, would work for renaming the blank elements:
import com.codecommit.antixml._
val q = <text>The capitals of Bolivia are <blank/> and <blank/>.</text>.convert
(q \\ "blank").map(_.copy(name = "input")).unselect
很遗憾,以下方法不起作用:
Unfortunately the following doesn't work:
(q \\ "blank").zipWithIndex.map { case (el, i) => el.copy(
name = "input",
attrs = Attributes("name" -> "blank.%d".format(i + 1))
)}.unselect
当然,因为一旦我们对拉链进行了 zipWithIndex
-ed,我们就不再有拉链,只有一个 IndexedSeq
——我们不能有 Zipper[(Node, Int)]
因为定义是 trait Zipper[+A <: Node] ...
.
Because of course once we've zipWithIndex
-ed the zipper we no longer have a zipper, just an IndexedSeq
—we can't have a Zipper[(Node, Int)]
because the definition is trait Zipper[+A <: Node] ...
.
是否有一种干净的方法可以在 Anti-XML 拉链上使用 zip
或 zipWithIndex
,使用 map
等进行一些其他操作?,最后得到的东西仍然是拉链?
Is there a clean way to use zip
or zipWithIndex
on an Anti-XML zipper, do some other operations with map
, etc., and end up with something that's still a zipper?
推荐答案
我想不出一种直接的方法来实现你所需要的,但是如果你愿意去使用一个较低级别的功能,你可以使用一个折叠
,例如:
I can't think of a direct way to achieve what you need, but if you're willing to go for a lower level function, you can use a fold
, e.g.:
val blanks = q \\ "blank"
(0 until blanks.size).foldLeft(blanks) {case (z, i) => z.updated(i, z(i).copy(
name = "input",
attrs = Attributes("name" -> "blank.%d".format(i + 1)))
)}.unselect
请注意,zipper 是一个随机访问容器,因此在这种情况下效率应该不是问题.
Note that the zipper is a random access container, so efficiency shouldn't be an issue in this case.
这篇关于在 Anti-XML 中压缩拉链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!