Re: getting XPath in REXML to dive deeper



Gavin Kistner wrote:
Peter Bailey wrote:
I'm finally using REXML instead of plain vanilla Ruby to convert XML
data. I've learned a bit about XPath and how REXML can use it. I've got
a sample XML file whose root element is <registration>. Underneath that
element are a bunch of children elements. So, when I do the instruction
listed below, I get all of them. But, I don't get the last two elements
that, to me anyway, are children, too. The only thing different about
them is that they have children, too. Does anyone know the nomenclature
for getting ALL of the children of an element, not just those that are
immediate children. I guess I want grandkids, too!

XPath.each(doc, "//registration/*") { |element| puts element.text }

The term you are looking for is 'descendants'. In XPath, '/' is
shorthand for the 'child' axis. You want the 'descendant' axis, which
has the shorthand (that you already used) of '//'.

So "//registration//*" is "At any level, find an element named
'registration', and then find every element below any of them."

You possible want "registration//*" which is "every element in the
entire XML file at every level except for the root node", but that seems
rather pointless. (Why have a hierarchy in the XML if you're throwing
away the meaning?) Perhaps that will work for you, or perhaps you should
think further about exactly what elements you really want to traverse.

Hope that helps (oh, and this has nothing to do with Ruby, BTW :p)
Gavin

Yes, this helps. Thanks, Gavin. What I did, to get everything I needed
was this:

XPath.each(doc, "//registration//*") { |element| puts element.text }

I just put in a second "/" after registration. That picked up ALL of the
descendants, and the grand-child elements, or, whatever they're called.
My only problem is that the output is kind of spacey, with a lot more
white space before those grand-child elements. -Peter
--
Posted via http://www.ruby-forum.com/.

.



Relevant Pages

  • Re: getting XPath in REXML to dive deeper
    ... I've learned a bit about XPath and how REXML can use it. ... a sample XML file whose root element is. ... shorthand for the 'child' axis. ...
    (comp.lang.ruby)
  • Re: Flexible __autoload scheme
    ... > Berislav Lopac wrote: ... >> above, or finding in an XML file using XPath and SimpleXML, assuming ... Because array must be iterated through, while a DOM document can be searched ... via XPath by a simple /*/classname (or something; ...
    (comp.lang.php)
  • Beginners-problem with xpath filter and XML
    ... so forgive me if this is a novice question. ... I am planning to use an XML file called SiteSettings.xml to hold ... U use a datalist and a xmldatasource to display the data. ... I need an xpath that displays only the correct data. ...
    (microsoft.public.dotnet.xml)
  • Re: Write to XML with C#
    ... Andrew Faust ... I am writing into xml file like follows: ... XmlTextWriter writer = new ... //Write the root element ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DBIx::XML::DataLoader
    ... of tables that correspond to the structure of the XML file. ... obviously not all columns in the database are keys or indeces. ... have no idea what XPATH is, let alone what to do with it. ...
    (comp.lang.perl.misc)