gpt4 book ai didi

html - TFHpple - iOS 中的 xpath - 让两个 parent 站起来

转载 作者:行者123 更新时间:2023-11-29 04:10:00 26 4
gpt4 key购买 nike

想要让两个 parent (曾祖 parent )向上还是让两个 child 向下?

<table style="background-color: #008000; border-style: none;" border="0" cellpadding="2" cellspacing="2">
<tr>
<td>
<img height="5" width="5" border="0" src="https://spacer.gif" title="07:00,24hrs: B Shift /.../E704/RS704/Firefighter #2" alt="07:00,24hrs: B Shift /.../E704/RS704/Firefighter #2">
</td>
</tr>
</table>
<img height="2" width="2" border="0" src="https://spacer.gif" alt="">
<img alt="" height="1" width="1" border="0" src="https://spacer.gif">
</td>
<TD ALIGN="RIGHT" VALIGN="TOP" width="17%">
<a href="javascript:void(0)" title="01/15/2013" class="daylink" onClick="return DayClick('01/15/2013');">15</a>
</TD>
<td rowspan="2" width="5">
<img alt="" height="1" width="1" border="0" src="https://spacer.gif">
</td>
</tr>
<tr>
<TD COLSPAN="2">
<TABLE>
<TR>
<TD style="background-color: #41FFB9; " class="calexception">
<a href="javascript:void(0)" onClick="return ShowRemoveExceptionWindow(&quot;4A30E80.fre01&quot;,&quot;3280530&quot;);" title="10hrs DetNonEMSStud(10), 07:00 - 17:00" style="color: #000000; text-decoration: none; font-weight: bold;">DetNonEMSStud(10)</a>
</TD>
</TR>
</TABLE>

iOS:

NSString *tutorialsXpathQueryString = @"//table/tr/td";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSLog(@"here is url: %@", tutorialsNodes);

NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {

Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];

tutorial.url = [element objectForKey:@"style"];
tutorial.title = [[element firstChild] objectForKey:@"title"];

我从 <td> 获取风格和标题来自 <a>正好我还需要从 <table> 获取样式

我对 obj-c 很陌生,第一次尝试使用 XPATH,任何示例都很棒!

最佳答案

我没有使用过 TFHpple,但如果它支持标准 XPath,您应该能够使用以下 XPath 从 td 转到其包含表:

ancestor::table[1]

此 XPath 选择作为上下文节点的祖先的最近表,因此如果您有此标记:

<table>
<tr><td></td></tr>
<tr>
<td>
<table><tr><td></td></tr></table>
<table>
<tr>
<td>Hey!</td>
</tr>
</table>
</td>
</tr>
</table>

您的上下文节点是带有文本“Hey!”的 td,那么上面的 XPath 将选择第 6 行的表。

看起来 TFHpple 没有提供在上下文节点上评估 XPath 的方法。鉴于此,一个新的建议 - 每个元素只有一个父元素,因此如果您继续向上查找父元素,您最终应该找到该表。向下移动要困难得多,因为一个元素可以有任意数量的直接子元素,每个元素都有自己的子元素集。我不太了解 Objective-C,但如果您可以确定该表仅向上两级,那么类似这样的操作可能会起作用:

TFHppleElement *table = [[element parent] parent];

如果不能保证表向上两层,那么应该有某种方法通过父级向上查找表。这是伪代码,但希望您能明白:

for (TFHppleElement *element in tutorialsNodes) {

Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];

tutorial.url = [element objectForKey:@"style"];
tutorial.title = [[element firstChild] objectForKey:@"title"];

TFHppleElement *table = [element parent];
while(table != null && [table tagName] != "table") {
table = [table parent]
}

// table should either be the parent table at this point,
// or null if there was no parent table.

关于html - TFHpple - iOS 中的 xpath - 让两个 parent 站起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com