用于在单个表达式中搜索多个标记的Xpath表达式

时间:2012-03-01 06:26:39

标签: xml xpath

我有以下XML示例 -

    <World>
    <Continents>
    <Continent>America</Continent>
    <Cities>
            <Country>USA</Country>
            <City>New York</City>
    </Cities>
    <Description>Newyork is a nice city</Description>
    <Cities>
            <Country>USA</Country>
            <City>Philadelphia</City>
    </Cities>
    <Description>Philadelphia is a nice city</Description>
    <Cities>
            <Country>USA</Country>
            <City>New Orleans</City>
    </Cities>
    <Description>New Orleans is a nice city</Description>
    </Continents>
    <Continents>
    <Continent>Asia</Continent>
    <Cities>
            <Country>India</Country>
            <City>Mumbai</City>
    </Cities>
    <Description>Mumbai is a nice city</Description>
    <Cities>
            <Country>India</Country>
            <City>New Delhi</City>
    </Cities>
    <Description>New Delhi is a nice city</Description>
    <Cities>
            <Country>India</Country>
            <City>Kolkata</City>
    </Cities>
    <Description>Kolkata is a nice city</Description>
    <Cities>
            <Country>Japan</Country>
            <City>Tokyo</City>
    </Cities>
    <Description>Tokyo is a nice city</Description>
    </Continents>
    </World>

我想列出以下条件的说明 -

 Country="India" OR City="New York"
 Continent="Asia" OR Country="Japan"

1 个答案:

答案 0 :(得分:1)

尝试下一个:

//Description[
  (preceding-sibling::Cities[1]/Country = "India" or 
   preceding-sibling::Cities[1]/City = "New York"
  ) or 
  (preceding-sibling::Continent[1] = "Asia" or 
   preceding-sibling::Cities[1]/Country = "Japan"
  )
]

我的测试中的输出(--NODE--是我的工具添加的文字文字):

<Description>Newyork is a nice city</Description>-- NODE --
<Description>Mumbai is a nice city</Description>-- NODE --
<Description>New Delhi is a nice city</Description>-- NODE --
<Description>Kolkata is a nice city</Description>-- NODE --
<Description>Tokyo is a nice city</Description>