Perl LibXML get last line number of Node

2019-09-09 07:51发布

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条回答
干净又极端
2楼-- · 2019-09-09 08:36

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!

查看更多
登录 后发表回答