1

我正在尝试使用 xml-conduit 和 xml-lens 来解析和遍历 XML 文档。不必多次遍历文档的相同部分,我想将遍历存储到前一点,然后进一步向下钻取。

前任。

let pos = doc ^. root . el "foo"
    bar = pos . text
    baz = pos ./ el "quux" . text

当我尝试这样做时,我收到以下错误:

No instance for (Data.Monoid.Monoid Element)
  arising from a use of `el'
Possible fix:
  add an instance declaration for (Data.Monoid.Monoid Element)
In the second argument of `(.)', namely `el "foo"'
In the second argument of `(^.)', namely `root . el "foo"'
In the expression: doc ^. root . el "foo"

我能做些什么来存储这个中间位置?

4

1 回答 1

1

发生类型错误是因为elTraversal. 遍历指向多个值(在这种情况下,所有foo元素)。(^.)另一方面,总是返回一个值。仅当目标类型是 a 时才使用(^.)with a ;在这种情况下,多个值使用. 但是,它不是一个(因为没有明智的方法将 XML 元素组合成单个元素),因此不起作用。要获取匹配的 XML 元素,您应该使用它,它将以列表的形式返回它们。TraversalMonoidmappendElementMonoid(^.)(^..)

至于正确的问题(“我可以做些什么来存储这个中间位置?”),Traversal是对位置的引用,所以如果你想进一步组合它,你不想用(^.)or来“取消引用”它(^..)。以下应该有效:

let pos = root . el "foo"
    baz = pos ./ el "quux" . text
    elemTexts = doc ^.. baz
于 2014-05-01T04:28:12.657 回答