Javascript遍历表以获取第一个列表中的值

时间:2019-05-02 09:42:49

标签: javascript html xpath

我正在尝试通过xpath javascript遍历这行代码,但我似乎无法获得第一个li标记的值,即Thesis [Master's-University,2012。“:

<table id="addInformationGraphics" cellpadding="2" cellspacing="0" width="100%">
<TR><td background="/images/icons/general/thickline.gif" nowrap vAlign='top'>
<span class='ColRow'>&nbsp;&nbsp;</span><span class="SectionHeader" ><a name = 'anchorAdditionalInfo'></a>
Additional Info
</span>
</td>
<A name="AdditionalInfo"></A></TR></TABLE>
<table id="addInformation" cellpadding="2" cellspacing="0"><tr>
<td vAlign="top"><IMG SRC="/images/icons/general/spacer.gif" width="20" height="1"></td>
<td class="ColRow"><ul><li>
 Thesis [Master&#39;s) -- University, 2012.</li></ul>     
</td>
</tr>

这是我正在尝试的人之一:

  var getaddInformation = document.evaluate("table[contains(@id,'addInformation')]/tbody/tr/td/following- sibling::td/ul/li", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
if (getaddInformation.singleNodeValue) {
var addInformationDetails = getaddInformation.singleNodeValue.textContent;
       }
console.log(addInformationDetails);
    var getaddInformation = document.evaluate("table[contains(@id,'addInformationGraphics')]/following-sibling::table/td/following-sibling::td/ul/li", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
if (getaddInformation.singleNodeValue) {
  var addInformationDetails = getaddInformation.singleNodeValue.textContent;
               }
console.log(addInformationDetails);

还有一个:

var getaddInformation = document.evaluate("table[contains(@id,'addInformationGraphics')]/following-sibling::table/td/following-sibling::td/ul/li", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
if (getaddInformation.singleNodeValue) {
  var addInformationDetails = getaddInformation.singleNodeValue.textContent;
       }
console.log(addInformationDetails);

为简单起见,这是xpath:

"table[contains(@id,'addInformation')]/tbody/tr/td/following-sibling::td/ul/li
And the other one:
 table[contains(@id,'addInformationGraphics')]/following-sibling::table/td/following-sibling::td/ul/li

如果我没记错的话,我可能会缺少什么,HTML文档的层次结构如下所示:

table 
  tbody 
    tr 
      td
      td
        ul
        li

谢谢!

1 个答案:

答案 0 :(得分:0)

使用evaluate API时,第二个参数是上下文节点,因此,如果要在HTML文档中找到HTML table,通常将document.body与相对路径一起使用像table一样找到table的{​​{1}}个孩子:

body
var getaddInformation = document.evaluate("table[contains(@id,'addInformation')]/tbody/tr/td/ul/li", document.body, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );

if (getaddInformation.singleNodeValue) {
  var addInformationDetails = getaddInformation.singleNodeValue.textContent;
  console.log(addInformationDetails);
}

或者您需要更改XPath以开头,例如<table id="addInformationGraphics" cellpadding="2" cellspacing="0" width="100%"> <TR><td background="/images/icons/general/thickline.gif" nowrap vAlign='top'> <span class='ColRow'>&nbsp;&nbsp;</span><span class="SectionHeader" ><a name = 'anchorAdditionalInfo'></a> Additional Info </span> </td> <A name="AdditionalInfo"></A></TR></TABLE> <table id="addInformation" cellpadding="2" cellspacing="0"><tr> <td vAlign="top"><IMG SRC="/images/icons/general/spacer.gif" width="20" height="1"></td> <td class="ColRow"><ul><li> Thesis [Master&#39;s) -- University, 2012.</li></ul> </td> </tr> </table>/html/body/table

但是请注意,要求“第一”列表并使用//table并不会产生一致的结果,如果您要根据文档顺序查找内容,请使用XPathResult.ANY_UNORDERED_NODE_TYPE