1

我正在尝试解析如下所示的 XML:

<h1>Collection A</h2>
<table>
  <tr>Property 1</tr>
  <tr>Property 2</tr>
</table>

<h2>Collection 2</h2>
<table>
  <tr>Property 1</tr>
  <tr>Property 88</tr>
</table>

我想这样解析该信息:

MyClass "Collection 1" "Property 1"
MyClass "Collection 1" "Property 2"
MyClass "Collection 2" "Property 1"
MyClass "Collection 2" "Property 88"

我不知道该怎么做。我的第一个想法是做类似的事情element "h1" $| followingSibling &// element "tr" &/ content,但这不起作用,因为它会捕获所有的 tr,即使是那些不“属于”我试图从中读取的表的,我不会无法知道哪些属性属于哪个集合。

我该如何解决这个问题?

4

1 回答 1

1

您必须定义自己的“直接兄弟”的 XML 轴作为followingSibling返回上下文之后的每个节点。这是可能的,因为AxisinText.XML.Cursor是 的类型同义词Cursor -> [Cursor]

immediateSibling = take 1 . (anyElement <=< followingSibling)

并且组合来自不同级别的信息只是嵌套列表理解:

selected = root $/ selector
selector = element "h2" >=> toTuple

-- replace tuple with your constructor
toTuple c = [ (coll, prop)
            | coll <- c $/ content
            , prop <- c $| (immediateSibling >=> element "table" &/ element "tr" &/ content) ]
于 2016-03-08T13:08:56.683 回答