如何通过IMPORTXML的XPath查询获取<tr>下的独立<td>子节点?
Let's tackle your issue step by step. The core problem here is that Google Sheets tends to render sibling <td> elements from the same <tr> as adjacent cells in a single row, rather than splitting them into separate rows. Here's how to fix it:
1. Transpose Results to Vertical Rows
The simplest fix is to directly target the <td> elements under your specific <tr>, then use TRANSPOSE to flip the horizontal results into vertical rows.
Try this formula:
=TRANSPOSE(IMPORTXML("https://www.dotabuff.com/matches/5011379854","//tr[contains(@class,'9764136')]/td"))
Why this works:
- The XPath
//tr[contains(@class,'9764136')]/tdexplicitly selects every<td>that's a direct child of your target<tr>(no extra parent checks needed). TRANSPOSEconverts the horizontal array of<td>values (which would normally fill a single row) into a vertical list, with each value in its own row.
2. Split Concatenated Text (If Transpose Doesn't Work)
If you're still seeing values merged into one cell, you can first join all <td> text with a newline character, then split it into separate rows:
=SPLIT(TEXTJOIN(CHAR(10), TRUE, IMPORTXML("https://www.dotabuff.com/matches/5011379854","//tr[contains(@class,'9764136')]/td")), CHAR(10))
How this works:
TEXTJOIN(CHAR(10), TRUE, ...)combines all<td>contents into a single string, separated by line breaks.SPLIT(..., CHAR(10))breaks that string apart at each line break, automatically placing each segment into its own row.
Quick Note on Your Original XPath
Your original formula //td[parent::tr[contains(@class,'9764136')]] does select the correct <td> elements, but Google Sheets displays sibling <td>s from the same <tr> as side-by-side cells. The transpose or split methods above reformat this output to match your "each on its own row" requirement.
If the contains(@class,'9764136') is too broad (risking matching other rows with similar class names), use an exact match instead:
//tr[@class='9764136']/td
Just make sure the class name is exactly 9764136 (no extra spaces or classes) for this to work.
内容的提问来源于stack exchange,提问作者Lemth

