2012-09-15 52 views
2

我有下面的XML:XPath表達式不正常

<thoughts> 
    <thought> 
     <id>1</id> 
     <category>Leadership</category> 
     <what>sometext</what>    
     <who>sometext</who> 
    </thought> 
    <thought> 
     <id>2</id> 
     <category>Leadership</category> 
     <what>sometext</what>    
     <who>sometext</who> 
    </thought> 
    ... 100s of category Leadership 
    <thought> 
     <id>1</id> 
     <category>Love</category> 
     <what>sometext</what>    
     <who>sometext</who> 
    </thought> 
    <thought> 
     <id>2</id> 
     <category>Love</category> 
     <what>sometext</what>    
     <who>sometext</who> 
    </thought> 
    ... 100s of category Love 

    ... and so on up to about ten categories 
</thoughts> 

我想選擇一個思想(什麼)對於給定的ID和類別。我正在用Java來做這件事。我試過如下:

"/thought[id='1']/thought[category='Love']/what/text()" 

的Java:

XPathFactory factory = XPathFactory.newInstance(); 
XPath xPath = factory.newXPath(); 
XPathExpression expr1 = xPath.compile("/thought[id='1']/thought[category='Love']/what/text()"); 
Object result1 = expr1.evaluate(doc, XPathConstants.NODESET); 
NodeList nodes1 = (NodeList) result1; 

我也曾嘗試以下XPathExpressions:

/thoughts/thought[id='1']/thought[category=`Love`]/what/text() 

我是新來的XML和XPath。

回答

0

我建議以下XPath上的表達:

/thoughts/thought[id='1' and category='Love']/what/text() 

,我可以推薦這個tutorial

+0

返回'NULL'。我沒有找到很多幫助的教程。請幫忙。 – harshit

+0

根據http://www.xpathtester.com/test XPath表達式應該沒問題 –

+0

是第一個回答。 – harshit

1

使用

/*/thought[id = '1' and category = 'Love']/what 

這將選擇任何what元素是thought的孩子元素,它有一個id子字符串值e "1",並且其子字符串值爲"Love",並且該子元素(thought元素)是XML文檔頂部元素的子元素。

如果你需要以上選擇的元素(S)的文本子節點,使用

/*/thought[id = '1' and category = 'Love']/what/text() 

如果你只需要(第一)的字符串值,上面的文字節點(S),使用

string(/*/thought[id = '1' and category = 'Love']/what) 

XSLT - 基於驗證

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <xsl:copy-of select="/*/thought[id = '1' and category = 'Love']/what"/> 
============ 
    <xsl:copy-of select="/*/thought[id = '1' and category = 'Love']/what/text()"/> 
============ 
    <xsl:copy-of select="string(/*/thought[id = '1' and category = 'Love']/what)"/> 
</xsl:template> 
</xsl:stylesheet> 

當該變換被應用在下面的XML文檔(所提供的一個具有用於想要的節點不同的值):

<thoughts> 
    <thought> 
     <id>1</id> 
     <category>Leadership</category> 
     <what>sometext</what> 
     <who>sometext</who> 
    </thought> 
    <thought> 
     <id>2</id> 
     <category>Leadership</category> 
     <what>sometext</what> 
     <who>sometext</who> 
    </thought> 
    ... 100's of with category Leadership  
    <thought> 
     <id>1</id> 
     <category>Love</category> 
     <what>some Love text 1</what> 
     <who>sometext</who> 
    </thought> 
    <thought> 
     <id>2</id> 
     <category>Love</category> 
     <what>sometext</what> 
     <who>sometext</who> 
    </thought> 
     ... 100's of with category Love 
     ... and so on up to about ten categories 
</thoughts> 

三個的XPath表達式,並且這些評價的結果被複制到輸出,通過一個方便的定界符視覺分離:

<what>some Love text 1</what> 
============ 
    some Love text 1 
============ 
    some Love text 1 

更新

在評論中OP增加了,不僅what,也who應選擇的要求。

下面是這種情況下相應的新XPath表達式:

/*/thought[id = '1' and category = 'Love']/*[self::what or self::who] 

/*/thought[id = '1' and category = 'Love']/*[self::what or self::who]/text() 
+0

@harshit,此答案中的XPath表達式滿足您的所有要求。 –