Perl LibXML get last line number of Node

99封情书 提交于 2019-12-11 18:12:53

问题


I use LibXML in Perl, which store the start line number of each node, but how i can get the last one?

I tried get last line number through..

  • ..counting newlines in innerhtml of the node, but LibXML return the innerhtml in different formatting than original, so that the line number differ.
  • ..node->getLastChild->line_number, but also havin no success.

Any Idea?


回答1:


If line_number returned the first line of a node as you say, all you'd need is

my $s_line_num = $node->line_number();
my $e_line_num = $node->nextSibling()->line_number();

But it doesn't. What line_number returns is actually closer the number of the last line of the node. For that, we could simply look at the previous sibling's line number.

my $s_line_num = $node->previousSibling()->line_number();
my $e_line_num = $node->line_number();

But while that's what it returns for non-element nodes, it returns the last line number of the start tag (rather than of the element as a whole) for elements. That's completely useless.

Sorry, no can do!



来源:https://stackoverflow.com/questions/14414738/perl-libxml-get-last-line-number-of-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!