2015-09-16 25 views
1

我使用TFHpple一些麻煩,第一個層次,所以這裏是: 我想解析以下行:獲取使用TFHpple

<div class=\"head\" style=\"height: 69.89px; line-height: 69.89px;\"> 
    <div class=\"cell editable\" style=\"width: 135px;\"contenteditable=\"true\"> 
     <p>&nbsp;1</p> 
    </div> 
    <div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\"> 
     <p>2</p> 
    </div> 
</div> 

<div style=\"height: 69.89px; line-height: 69.89px;\" class=\"head\"> 
    <div class=\"cell\" style=\"width: 135px; text-align: left;\"contenteditable=\"false\"> 
     <p>3&nbsp;</p> 
    </div> 
    <div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\"> 
     <p>4</p> 
    </div> 
</div> 

<div style=\"height: 69.89px; line-height: 69.89px;\" class=\"\"> 
    <div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\"> 
     <p>5</p> 
    </div> 
    <div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\"> 
     <p>6</p> 
    </div> 
</div> 

現在我想提出的第一級的div「元素「(對不起,我不知道正確的術語)。 所以我試圖通過簡單地將/div作爲xPath到searchWithXPathQuery方法來實現,但它根本找不到任何東西。

我的第二個解決方案是嘗試使用這種路徑://div[@class=\"head\"],但也允許[@class=\"\"],但我甚至不知道是否有可能。 (我想這樣做,因爲我需要的元素與數據中的排列順序相同)

所以,這裏是我的問題,是否有一個特定的原因,爲什麼TFHpple不會工作與/div? 如果現在只需要第一級div,那麼是否可以使用xPath對屬性的值進行謂詞(這裏是屬性class)? (以及如何?我現在看了很多,找不到任何東西)

感謝您的幫助。

PS:如果有幫助,這是我用它來嘗試解析數據的代碼,它首先將包含字符串self.material.Text在:

NSData * data = [self.material.Text dataUsingEncoding:NSUnicodeStringEncoding]; 
TFHpple * tableParser = [TFHpple hppleWithHTMLData:data]; 
NSString * firstXPath = @"/div"; 
NSArray<TFHppleElement *> * tableHeader = [tableParser searchWithXPathQuery:firstXPath]; 
NSLog(@"We found : %d", tableHeader.count); 

回答

0

您寫道:

獲取使用TFHpple

第一級我假定你的意思是:沒有也越來越所有的後裔?

以你的其他要求綜合考慮,可以採取以下方式:

//div[not(ancestor::div)][@class='head' or @class=''] 

剖析這一點:

  • 選擇所有div元素;在任何級別(是的,正確的說法)整個文檔://div
  • 通過檢查是否存在某個div祖先(父親是父親的父親),過濾包含不包含div的元素本身的謂詞(括號內的內容)[not(ancestor::div)]
  • 過濾器由您的其它要求:[@class='head' or @class='']

注1:您給出的XML是無效的,它包含多個根元素。 XML最多隻能有一個根元素。

注2:如果您的要求是:首先得到由@class或空@class所有div,然後只有那些「第一級」,扭轉斷言:

//div[@class='head' or @class=''][not(ancestor::div)] 
0

您可以使用下面的XPath表達式得到div元素 - 這就是一個相當正確的條款而─,其class屬性值等於"head"或空:

//div[@ciass='head' or @class=''] 
+0

感謝您答案,它確實解決了我的一部分問題,但事實是,有時'class'屬性在空時會丟失(我無法解決這個問題,我從Web平臺獲取文件),有沒有辦法我仍然可以接受這個元素? (相當於'/ div'的東西可能?) –

+0

@HuguesDuvillier,如果它是空的,這個解決方案也可以工作。在XPath 1.0中,空節點的字符串值是一個空字符串(在XPath 2.0中稍有變化)。如果你還想檢查空格,請使用'normalize-string(@class)'。 – Abel