开发者

Can XML::LibXML add nodes to a document based on XPath alone?

I'm looking for a method that allows me to spring to life a text node that something like:

$doc->addNode( $xPath );

The fact that XPath queries can get quite funky at times is 开发者_Python百科probably a good enough reason for not implementing such a method inside XML::LibXML.

But the simpler, absolute XPaths do provide a convenient address representation for nodes. There are times when such a method makes life a whole lot easier, as well as a whole lot more sense.

To address this need, I rolled my own overly-simplistic sub that performs a recursive walk up the XPath until an existing XPath is found, at which point the necessary nodes are created and added. While it works for the time being, it doesn't handle indexed XPaths (like /library/book[2]/title) and references to text() nodes need to be stripped out.

I'm hoping there is an alternative solution that fulfills my requirement. Writing my own XPath-parser/node-generator is way too much work for what it's worth.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜