如何从xml类型提供程序引用第一个节点

时间:2015-07-29 11:11:06

标签: xml f# type-providers f#-data

我似乎无法仅从xml类型提供程序中选择第一个地址邮政编码。

下面的片段是来自更大的XML文档的片段:

                <applicant>
                    <address>
                        <buildingno>8</buildingno>
                        <street1>Bob Way</street1>
                        <locality>Jones</locality>
                        <posttown>Somewhere</posttown>
                        <postcode>AB12 3QE</postcode>
                    </address>
                    <address>
                        <buildingno>9</buildingno>
                        <street1>SomeStreet</street1>
                        <posttown>Somewhere</posttown>
                        <postcode>AB13 4FE</postcode>
                    </address>
                </applicant>

我已将文档加载到类型提供程序中:

type XmlReqSample = XmlProvider<"C:\\Temp\\requests\\samples.xml", SampleIsList=true, Global=true>

有些请求只有一个地址,有些有两个或更多,这就是我尝试加载样本的原因。我想知道如何只选择邮政编码的第一个地址:

// 'file' is a string of XML data
let doc = XmlReqSample.Parse(file)
let postCode = doc.Body.Applicant.Address.Value.Postcode.Value

但是,我不能只选择第一个邮政编码。

如何选择第一个地址,特别是邮政编码?

1 个答案:

答案 0 :(得分:1)

let getPostCode address =
  address |> Option.bind (fun (t:XmlReqSample.Address) -> Some t.Postcode)  
  

val firstAddess:地址选项

此外,要从地址获取邮政编码,您可以定义一个小功能:

getPostCode firstAddress

并使用如:

function menu(){
    title="Question time"
    prompt="Select:"
    options=("Yes" "No" "Maybe")
    echo "$title"
    PS3="$prompt"
    select opt in "${options[@]}" "Quit/Cancel"; do
        case "$REPLY" in
            1 ) echo "You picked $opt which is option $REPLY";;
            2 ) echo "You picked $opt which is option $REPLY";;
            3 ) echo "You picked $opt which is option $REPLY";;
            $(( ${#options[@]}+1 )) ) clear; echo "Goodbye!"; exit;;
            *) echo "Invalid option. Try another one.";continue;;
         esac
     done
     return
}
  

val it:string option = Some&#34; AB12 3QE&#34;