I use nokogiri(which in turn uses xpath) to parse xml file. xml has some data including “definition” for every element. Some of the child nodes does not have definition element.
Sample Example:
<parent>
<definition> It is XYZ </definition>
</parent>
<parent>
<definition> It is XYZ </definition>
</parent>
<parent>
</parent>
<parent>
<definition> It is XYZ </definition>
</parent>
XPath like this ‘/parent/definition’ will return an array only with 3 elements.
Here, 3rd element does not have definition element. I am querying for all the definitions. I want an array as output which will have first, second and forth element as “It is XYZ” and 3rd element should be empty string or nil. But I am getting only 3 elements in array with values of 1,2 and 4. I use this array later in my code. I want the third element to be blank. It is possible to achieve it?
Not sure if it matters, but xml contents keep changing every time I make a call. I mean, tags will be same but the number of elements and values will change. XML I am parsing is pretty big. One solution is to do querying each element in a loop. It worked fine but it takes too much time. So any solution with which I can get all definitions in one parse will be really helpful. Thanks.
In XPath 2.0:
In XPath 1.0 – oh dear, why would anyone want to use XPath 1.0? ;-(