Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2114

Re: Can you search in REXML by attributes?

From "Kyle X." <haebooty@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Can you search in REXML by attributes?
Date 2011-04-01 11:27 -0500
Organization Service de news de lacave.net
Message-ID <07d88b73a8b6b59812b5fed98c782aca@ruby-forum.com> (permalink)
References <6680e1dd986ba2ce87d806950a81ee57@ruby-forum.com> <BANLkTim=ZwXUL-1hoLvfDzN8EUCUQDk1Vw@mail.gmail.com>

Show all headers | View raw


Robert K. wrote in post #990336:
> On Fri, Apr 1, 2011 at 2:53 AM, Kyle X. <haebooty@yahoo.com> wrote:
>
> It is not entirely clear what you want.  Do you want to look for all
> "ref" instances and find elements they are referring to?  Or do you
> want to do some kind of graph traversal where you start with a
> particular element and follow every ref attribute?

Hi and thank you for your help.  I am sorry if what I wrote was unclear. 
What my goal is is to start at a given location (in this case- 
<IfcWallStandardCase id="i1677">) and eventually grab the three 
IfcLengthMeasure text values, that are associated with this 
<IfcWallStandardCase id="i1677">, and put them into an array.

> If the latter you can for example do a BFS.
>
> 10:11:30 Temp$ ./rx.rb
> --- VISIT:
> <IfcAxis2Placement3D id='i1671'>
>  <Location>
>    <IfcCartesianPoint xsi:nil='true' ref='i1667'/>
>  </Location>
> </IfcAxis2Placement3D>
> --- VISIT:
> <IfcCartesianPoint id='i1667'>
>  <Coordinates exp:cType='list' id='i1670'>
>    <IfcLengthMeasure exp:pos='0'>117.4</IfcLengthMeasure>
>    <IfcLengthMeasure exp:pos='1'>119.7</IfcLengthMeasure>
>    <IfcLengthMeasure exp:pos='2'>0.</IfcLengthMeasure>
>  </Coordinates>
> </IfcCartesianPoint>
> 10:11:43 Temp$ cat -n rx.rb
>      1  #!/bin/env ruby19
>      2
>      3  require 'rexml/document'
>      4
>      5  doc = REXML::Document.new(DATA.read)
>      6
>      7  # BFS
>      8  queue = %w{i1671}
>      9
>     10  until queue.empty?
>     11    id = queue.shift
>     12
>     13    REXML::XPath.each(doc, "//*[@id='#{id}']") do |e|
>     14      puts "--- VISIT:", e
>     15
>     16      REXML::XPath.each(e, './/*[@ref]') do |child|
>     17        next_id = child.attribute('ref') and queue.push(next_id)
>     18      end
>     19    end
>     20  end
>     21
>     22  __END__
>     23  <root xmlns:exp="http://foo" xmlns:xsi="http://bar">
>     24  <IfcWallStandardCase id="i1677">
>     25   <ObjectPlacement>
>     26     <IfcLocalPlacement xsi:nil="true" ref="i1671"/>
>     27   </ObjectPlacement>
>     28  </IfcWallStandardCase>
>     29
>     30  <IfcAxis2Placement3D id="i1671">
>     31   <Location>
>     32     <IfcCartesianPoint xsi:nil="true" ref="i1667"/>
>     33   </Location>
>     34  </IfcAxis2Placement3D>
>     35
>     36  <IfcCartesianPoint id="i1667">
>     37   <Coordinates id="i1670" exp:cType="list">
>     38     <IfcLengthMeasure exp:pos="0">117.4</IfcLengthMeasure>
>     39     <IfcLengthMeasure exp:pos="1">119.7</IfcLengthMeasure>
>     40     <IfcLengthMeasure exp:pos="2">0.</IfcLengthMeasure>
>     41   </Coordinates>
>     42  </IfcCartesianPoint>
>     43  </root>
> 10:11:47 Temp$

I will give this a try.

>
> Kind regards
>
> robert

Dear 7stud.  Using the XPath command:

doc = Document.new xml
target = XPath.match(doc, "//*[@id = 'i1671']")
p target

It produces the following output as you said it would.
--output:--
[<IfcAxis2Placement3D id='i1671'> ... </>]

But I cannot figure out how to do anything with this from here to get to 
the next point, and eventually be able to grab the three values.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-03-31 19:53 -0500
  Re: Can you search in REXML by attributes? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-31 20:27 -0500
  Re: Can you search in REXML by attributes? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-31 20:45 -0500
  Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-01 11:27 -0500
    Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-01 12:19 -0500
      Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-10 18:01 -0500
        Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-10 18:27 -0500
          Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-11 02:37 -0500
            Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-11 13:57 -0500
              Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-12 03:17 -0500
                Re: Can you search in REXML by attributes? Robert Klemme <shortcutter@googlemail.com> - 2011-04-12 04:46 -0500
                Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-12 14:39 -0500
                Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-12 15:37 -0500
  Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-01 14:22 -0500
    Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-04 13:44 -0500
  Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-19 15:42 -0500
  Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-20 02:22 -0500

csiph-web