From: Robert Klemme Date: 2013-06-16T23:35:17+09:00 Subject: Re: NokoGiri XML Parser --20cf300fad7569e9e204df4663ca Content-Type: text/plain; charset=ISO-8859-1 On Fri, Jun 14, 2013 at 6:46 PM, Devender P. wrote: > The problem with xpath or maybe i am not sure how to use it but if i say > > doc.xpath("tlv componentInstances") produces the list of all attirbutes > inside the componentInstances without giving me the hierarchy. > > Here is what i have in my XML. > > Each cellname with a type "HIER" will have tag with > additional attributes. > > > A1 > HIER... > ... > A2 > HIER > > A3 > MODEL.... > > B2 > MODEL > > This is not valid XML. Where are the closing tags for and ? > I want to be able to capture the componentInstances with respect to the > parent cell. > You'll likely need a two step approach: search for all and for each one of them search for the closest anchestor . require 'nokogiri' def c_name(c_node) t = c_node.at_xpath('n/text()') and t.to_s end dom = Nokogiri.XML < foo bar bat XML_DOC dom.xpath('//c').each do |c_node| parent = c_node.at_xpath 'ancestor::c' if parent printf "Node: %s parent %s\n", c_name(c_node), c_name(parent) else printf "Node: %s no parent\n", c_name(c_node) end end > Please feel free to suggest a different XML structure if the paresers > can handle them better. I would start with _valid_ XML. You should model the XML primarily according to the requirements of the model that you need to created and not necessarily according to what will have the easiest XPath for retrieval. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --20cf300fad7569e9e204df4663ca Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Fri, Jun 14, 2013 at 6:46 PM, Devender P. <= lists@ruby-forum.= com> wrote:
The problem with xpath or maybe i am not sure how to use i= t but if i say

doc.xpath("tlv componentInstances") produces the list of all atti= rbutes
inside the componentInstances without giving me the hierarchy.

Here is what i have in my XML.

Each cellname with a type "HIER" will have <componentInstances= > tag with
additional attributes.


=A0 <CELLNAME>A1
=A0 <TYPE>HIER...
=A0 <COMPONENTINSTANCES>...
=A0 =A0 =A0 <CELLNAME>A2
=A0 =A0 =A0 <TYPE>HIER
=A0 =A0 =A0 <COMPONENTINSTANCES>
=A0 =A0 =A0 =A0 <CELLNAME>A3
=A0 =A0 =A0 =A0 <TYPE>MODEL....
=A0 =A0 =A0 =A0</COMPONENTINSTANCES>
=A0 =A0 =A0 <CELLNAME>B2
=A0 =A0 =A0 <TYPE>MODEL
=A0 =A0</COMPONENTINSTANCES>

This is not valid XML. =A0Where are the closing tags for <CELLNAME>= and <TYPE>?
=A0
I want to be able to capture the componentInstances with respect to the
parent cell.

You'll likely ne= ed a two step approach: search for all <CELLNAME> and for each one of= them search for the closest anchestor <CELLNAME>.

require 'nokogiri'

def c_name(c_node)
=A0 t =3D c_node.at_xpath('n/text()'= ;) and t.to_s
end

dom =3D Nokogiri.XML &= lt;<XML_DOC
<root>
=A0 <c>
=A0 =A0 <n>foo<= ;/n>
=A0 =A0 <c>
=A0 =A0 <n>bar</n>
=A0 =A0 <c>
=A0 =A0 =A0<n&g= t;bat</b>
=A0 =A0 </c>
=A0 =A0 </c>
=A0 </c><= /div>
</root>
XML_DOC

dom.xpath('//c').each do |c_n= ode|
=A0 parent =3D c_node.at_xpath 'ancestor::c'
=A0=A0
=A0 if parent
=A0 =A0 printf "Node: %s p= arent %s\n", c_name(c_node), c_name(parent)
=A0 else
=A0 =A0 printf "Node: %s no parent\n", c_= name(c_node)
=A0 end
end

=A0
Please feel free to suggest a different XML structure if the paresers
can handle them better.

I would start with _valid_ XML. =A0You should model the XML primarily a= ccording to the requirements of the model that you need to created and not = necessarily according to what will have the easiest XPath for retrieval.

Cheers

robert
=

--
remember.guy do |as, often| as.you_= can - without end
http://blog.rubybestpractice= s.com/
--20cf300fad7569e9e204df4663ca--