如何使用F#Data的XML Type Provider访问父节点的属性?

时间:2014-02-25 11:06:45

标签: xml f# parent f#-data xmldataprovider

如何使用F# Data's XML Type Provider访问父节点的属性?假设我有以下XML:

<?xml version="1.0" encoding="UTF-16"?>
<ROOT>
    <level1 name="level1Name1" x="8">
        <level2 name="level2Name1" a="8" b="104" />
        <level2 name="level2Name2" a="85" b="140" />
    </level1>
    <level1 name="level1Name2" x="10">
        <level2 name="level2Name3" a="50" b="500" />
        <level2 name="level2Name4" a="376" b="1065" />
        <level2 name="level2Name5" a="10" b="100" />
        <level2 name="level2Name6" a="700" b="700" />
    </level1>
    <level1 name="level1Name3" x="7">
        <level2 name="level2Name7" a="502" b="66" />
    </level1>
</ROOT>

我想在下面的代码中使用它:

open System
open System.Xml.Linq
open FSharp.Data

type myXmlType = XmlProvider<"""<?xml version="1.0" encoding="UTF-16"?><ROOT><level1 name="level1Name1" x="8"><level2 name="level2Name1" a="8" b="104" /><level2 name="level2Name2" a="85" b="140" /></level1><level1 name="level1Name2" x="10"><level2 name="level2Name3" a="50" b="500" /><level2 name="level2Name4" a="376" b="1065" /><level2 name="level2Name5" a="10" b="100" /><level2 name="level2Name6" a="700" b="700" /></level1><level1 name="level1Name3" x="7"><level2 name="level2Name7" a="502" b="66" /></level1></ROOT>""">

let myXml = myXmlType.Parse("""<?xml version="1.0" encoding="UTF-16"?><ROOT><level1 name="level1Name1" x="8"><level2 name="level2Name1" a="8" b="104" /><level2 name="level2Name2" a="85" b="140" /></level1><level1 name="level1Name2" x="10"><level2 name="level2Name3" a="50" b="500" /><level2 name="level2Name4" a="376" b="1065" /><level2 name="level2Name5" a="10" b="100" /><level2 name="level2Name6" a="700" b="700" /></level1><level1 name="level1Name3" x="7"><level2 name="level2Name7" a="502" b="66" /></level1></ROOT>""")

let howToDoIt =
    myXml.GetLevel1s()
    |> Seq.collect (fun L1 -> L1.GetLevel2s())
    |> Seq.tryFind (fun L2 -> L2.b = L1.x * L2.a) // *REMARK
    |> function
    | None -> ""
    | Some L2 -> L2.Name

* 备注:L1.x显然无效,但我想实现类似的目标。

1 个答案:

答案 0 :(得分:4)

我认为最简单的方法是在Seq.tryPick上使用L1,内部函数在当前L1的子项中搜索:

myXml.GetLevel1s()
|> Seq.tryPick (fun L1 ->
    L1.GetLevel2s()
    |> Seq.tryFind (fun L2 -> L2.b = L1.x * L2.a))
|> function
| None -> ""
| Some L2 -> L2.Name