簡體   English   中英

XSL Select 來自父元素的值,如果兩個子元素具有特定值

[英]XSL Select value from parent element, if two children have specific values

我有以下 XML。 如果存在一個address component元素,它同時具有值為“sublocality”的type元素和值為“sublocality_level_1”的type元素,那么我想 select 該address componentlong_name值。

在下面的示例 XML 中,這將是“Vleuten-De Meern”

我試過的:

    Dim myXML As New XmlDocument
    Dim responseString As String = "<GeocodeResponse><status>OK</status><result><type>political</type><type>sublocality</type><type>sublocality_level_2</type><formatted_address>De Meern, Utrecht, Netherlands</formatted_address><address_component><long_name>De Meern</long_name><short_name>De Meern</short_name><type>political</type><type>sublocality</type><type>sublocality_level_2</type></address_component><address_component><long_name>Vleuten-De Meern</long_name><short_name>Vleuten-De Meern</short_name><type>political</type><type>sublocality</type><type>sublocality_level_1</type></address_component><address_component><long_name>Utrecht</long_name><short_name>Utrecht</short_name><type>locality</type><type>political</type></address_component><address_component><long_name>Utrecht</long_name><short_name>Utrecht</short_name><type>administrative_area_level_2</type><type>political</type></address_component><address_component><long_name>Utrecht</long_name><short_name>UT</short_name><type>administrative_area_level_1</type><type>political</type></address_component><address_component><long_name>Netherlands</long_name><short_name>NL</short_name><type>country</type><type>political</type></address_component><geometry><location><lat>52.0757940</lat><lng>5.0319779</lng></location><location_type>APPROXIMATE</location_type><viewport><southwest><lat>52.0714898</lat><lng>5.0196140</lng></southwest><northeast><lat>52.0904459</lat><lng>5.0484669</lng></northeast></viewport><bounds><southwest><lat>52.0714898</lat><lng>5.0196140</lng></southwest><northeast><lat>52.0904459</lat><lng>5.0484669</lng></northeast></bounds></geometry><place_id>ChIJx5fkM1RlxkcR_pIQJLtl5Ro</place_id></result></GeocodeResponse>"
    myXML.LoadXml(responseString)

    If myXML.SelectSingleNode("GeocodeResponse/result[address_component[type[. = 'sublocality']] and address_component[type[. = 'sublocality_level_1']]]") IsNot Nothing Then
        Log("found")
        neighborhood = myXML.SelectSingleNode("GeocodeResponse/result[address_component[type[. = 'sublocality']] and address_component[type[. = 'sublocality_level_1']]]/long_name").InnerText            
    Else
        Log("NOT found")
    End If

找到該元素,記錄一個值“找到”,但是當我想檢索我得到的值時:

“對象引用未設置為 object 的實例。”

<GeocodeResponse>
    <status>OK</status>
    <result>
        <type>political</type>
        <type>sublocality</type>
        <type>sublocality_level_2</type>
        <formatted_address>De Meern, Utrecht, Netherlands</formatted_address>
        <address_component>
            <long_name>De Meern</long_name>
            <short_name>De Meern</short_name>
            <type>political</type>
            <type>sublocality</type>
            <type>sublocality_level_2</type>
        </address_component>
        <address_component>
            <long_name>Vleuten-De Meern</long_name>
            <short_name>Vleuten-De Meern</short_name>
            <type>political</type>
            <type>sublocality</type>
            <type>sublocality_level_1</type>
        </address_component>
        <address_component>
            <long_name>Utrecht</long_name>
            <short_name>Utrecht</short_name>
            <type>locality</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Utrecht</long_name>
            <short_name>Utrecht</short_name>
            <type>administrative_area_level_2</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Utrecht</long_name>
            <short_name>UT</short_name>
            <type>administrative_area_level_1</type>
            <type>political</type>
        </address_component>
        <address_component>
            <long_name>Netherlands</long_name>
            <short_name>NL</short_name>
            <type>country</type>
            <type>political</type>
        </address_component>
        <geometry>
            <location>
                <lat>52.0757940</lat>
                <lng>5.0319779</lng>
            </location>
            <location_type>APPROXIMATE</location_type>
            <viewport>
                <southwest>
                    <lat>52.0714898</lat>
                    <lng>5.0196140</lng>
                </southwest>
                <northeast>
                    <lat>52.0904459</lat>
                    <lng>5.0484669</lng>
                </northeast>
            </viewport>
            <bounds>
                <southwest>
                    <lat>52.0714898</lat>
                    <lng>5.0196140</lng>
                </southwest>
                <northeast>
                    <lat>52.0904459</lat>
                    <lng>5.0484669</lng>
                </northeast>
            </bounds>
        </geometry>
        <place_id>ChIJx5fkM1RlxkcR_pIQJLtl5Ro</place_id>
    </result>
</GeocodeResponse>

您的路徑似乎錯誤, long_nameaddress_component的孩子; 所以確保你 select 沿着這條路走,不要將相關元素放入謂詞中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM