XPath using starts-with function

117,035

Solution 1

Use:

//REVENUE_YEAR[starts-with(.,'2552')]/../REGION/text() 

Solution 2

Try this

//ITEM/*[starts-with(text(),'2552')]/following-sibling::*
Share:
117,035
Admin
Author by

Admin

Updated on February 05, 2020

Comments

  • Admin
    Admin over 4 years

    I'm writing Java on Android to retrieve data from XML file, but I've got a problem. Consider this XML:

    <ITEM>
       <REVENUE_YEAR>2554-02</REVENUE_YEAR>
       <REGION>Central</REGION>
    </ITEM>
    <ITEM>
      <REVENUE_YEAR>2552-02</REVENUE_YEAR>
      <REGION>Central</REGION>
    </ITEM>
    <ITEM>
      <REVENUE_YEAR>2552-03</REVENUE_YEAR>
      <REGION>Central</REGION>
    </ITEM>
    

    How can I get all elements in years that start-with 2552. I tried:

    //REVENUE_YEAR[starts-with(.,'2552')]/text()
    

    It works, but when I tried:

    //REVENUE_YEAR[starts-with(.,'2552')]/REGION/text() 
    

    it doesn't work.

  • Mogsdad
    Mogsdad over 8 years
    An answer with explanation, like this is better (and gets more votes) than a simple "try this" like that.