需要找到父节点id
这是我
<table id="1" style="width=100%">
<tr>
<td id="1">
<table id="2" style="width=50%">
<tr>
<td id="2">
</td>
</tr>
</table>
</td>
</tr>
</table>
使用 xslt1.0 的示例输入。当模板匹配“td”匹配时,我需要找到相应的表id值。例如,如果匹配id=1的td,我想从表(id=1)中获取样式属性值并且如果 id=2 的 td 匹配,我想从表(id=2)中获取样式属性值。我在模板中编写了 ancestor::table/@style
,但是两个 td 都引用了 id=1 的表格样式。
This is my sample input
<table id="1" style="width=100%">
<tr>
<td id="1">
<table id="2" style="width=50%">
<tr>
<td id="2">
</td>
</tr>
</table>
</td>
</tr>
</table>
I am using xslt1.0. when ever the template match 'td' is matched, i need to find the corresponding table id value..For example, If the td with id=1 is matched,i want to take style attribute value from table(id=1) and if the td with id=2 is matched,i want to take style attribute value from table(id=2). I have written ancestor::table/@style
in my template, but both td are referring the styles of table with id=1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你很接近。由于
ancestor
轴中可以有多个table
,因此您需要像ancestor::table[1]/@style< 中那样获取第一个表/代码>。当然,如果你绝对确定总有一个
table
链 ->tr
->td
(不是可选的tbody
)那么你可以接受@Flack的答案。You were close. Because there can be more than one
table
in theancestor
axis, you need to get the first one like inancestor::table[1]/@style
. Of course, if you are absolute sure there is always a chain oftable
->tr
->td
(not optionaltbody
) then you could go with @Flack's answer.假设您处于“td”上下文中,请使用此 XPath:
针对您的示例测试 XSLT:
结果:
Assuming you are in 'td' context, use this XPath:
Test XSLT against your sample:
Result:
尝试这个 XPath 它工作完美
结果:
Try this XPath it works perfectly
Result: