带条件的xpath

die*_*cht 3 xml xpath

如何使用复杂条件在Xpath中获取元素?

例如:

<?xml version="1.0" encoding="UTF-8"?>
<stock xmlns="http://localhost/aaabbb">
<item item-id="1">
 <name xml:format="short">This is a short name</name>
 <name xml:format="long">This is a LONG name</name>
</item>
</stock>
Run Code Online (Sandbox Code Playgroud)

目标:获取标签WHERE xml:format ="long"的文本.

在此先感谢您的帮助!

Mor*_*sen 6

看看这个:http://www.w3schools.com/xpath/xpath_syntax.asp.您要求的示例:

XML文档:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore> 
Run Code Online (Sandbox Code Playgroud)

XPATH:

//title[@lang='eng']    Selects all the title elements that have an attribute named lang with a value of 'eng'
Run Code Online (Sandbox Code Playgroud)

所以你应该这样做:

//name[@xml:format='long']
Run Code Online (Sandbox Code Playgroud)