[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Alberto Alberto

2/13/2009 4:09:00 PM

Hi all,

I have a question:

I would like to know if it is possible, using REXML, get full path from
root, of document XML, to leaf node, through a XPath query?


example with l'XML:

<primo>
<secondo>
<terzo id='3'>
<terzo/>
<secondo/>
<quarto>
...
<quarto/>
<primo>


Xpath: //terzo[@id='3']

Output:
//primo/secondo/terzo[id='3']

Thank's in advance!
Alberto
--
Posted via http://www.ruby-....

8 Answers

Phlip

2/13/2009 5:37:00 PM

0

Alberto Alberto wrote:

> Xpath: //terzo[@id='3']
>
> Output:
> /primo/secondo/terzo[id='3']

You meant / not //.

Try this:

REXML::XPath(doc, "//terzo[@id='3']").xpath

That's off the top of my head; advise me if it didn't work! I know there's a
..xpath or .path in there somewhere...

(And I suspect Nokogiri provides a better XML ride!)

--
Phlip

7stud --

2/13/2009 5:46:00 PM

0

Alberto Alberto wrote:
>
> I would like to know if it is possible, using REXML, get full path from
> root, of document XML, to leaf node, through a XPath query?
>
>
> example with l'XML:
>
> <primo>
> <secondo>
> <terzo id='3'>
> <terzo/>
> <secondo/>
> <quarto>
> ...
> <quarto/>
> <primo>
>
>
> Xpath: //terzo[@id='3']
>
> Output:
> //primo/secondo/terzo[id='3']
>

Is that really considered XML?

require "rexml/document"
include REXML

xml = <<TO_END_OF_XML
<primo>
<secondo>
<terzo id='3'>
</terzo>
</secondo>
<quarto>
...
</quarto>

<terzo id='3'>
</terzo>

</primo>
TO_END_OF_XML

doc = Document.new xml

target = "//terzo[@id='3']"
paths = []

XPath.each(doc, target) do |element|
parents = []

while true
parent = element.parent
break if parent.name == ""

parents << parent.name
element = parent
end

path = parents.reverse.join('/')
paths << "//#{path}#{target[1..-1]}"
end

puts *paths

--output:--
//primo/secondo/terzo[@id='3']
//primo/terzo[@id='3']

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

Phlip

2/13/2009 6:00:00 PM

0

> REXML::XPath(doc, "//terzo[@id='3']").xpath

Actually XPath.first

But I just tested that (indirectly), and the xpath accessor worked.

7stud --

2/13/2009 6:02:00 PM

0

Phlip wrote:
>
> Try this:
>
> REXML::XPath(doc, "//terzo[@id='3']").xpath
>
> That's off the top of my head; advise me if it didn't work! I know
> there's a
> .xpath or .path in there somewhere...
>

When I output element.xpath, I get:

/primo/secondo/terzo
/primo/terzo

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

Alberto Alberto

2/16/2009 11:13:00 AM

0

7stud -- wrote:
> Alberto Alberto wrote:
>>
>> I would like to know if it is possible, using REXML, get full path from
>> root, of document XML, to leaf node, through a XPath query?
>>
>>
>> example with l'XML:
>>
>> <primo>
>> <secondo>
>> <terzo id='3'>
>> <terzo/>
>> <secondo/>
>> <quarto>
>> ...
>> <quarto/>
>> <primo>
>>
>>
>> Xpath: //terzo[@id='3']
>>
>> Output:
>> //primo/secondo/terzo[id='3']
>>
>
> Is that really considered XML?
>
> require "rexml/document"
> include REXML
>
> xml = <<TO_END_OF_XML
> <primo>
> <secondo>
> <terzo id='3'>
> </terzo>
> </secondo>
> <quarto>
> ...
> </quarto>
>
> <terzo id='3'>
> </terzo>
>
> </primo>
> TO_END_OF_XML
>
> doc = Document.new xml
>
> target = "//terzo[@id='3']"
> paths = []
>
> XPath.each(doc, target) do |element|
> parents = []
>
> while true
> parent = element.parent
> break if parent.name == ""
>
> parents << parent.name
> element = parent
> end
>
> path = parents.reverse.join('/')
> paths << "//#{path}#{target[1..-1]}"
> end
>
> puts *paths
>
> --output:--
> //primo/secondo/terzo[@id='3']
> //primo/terzo[@id='3']



I try this solution and it works, but now my question is after operation
parents << parent.name

over parents array can I obtain a value of attributes with someting like
this?

.attributes.each_attribute {|attr|
p attr.expanded_name+" => "+attr.value
}

Thank's a lot for quickly support.
Alberto
--
Posted via http://www.ruby-....

Neil X

3/18/2011 9:43:00 PM

0

On Mar 18, 5:41 pm, Gladys <just.c...@say.it.anymore> wrote:
> On 3/18/2011 2:47 PM, mjd wrote:
>
>
>
> > Mickey too?  man I gotta get out more - both drummers gone and I had
> > no idea!
>
> They both got booted for wanting too much money.


And were given the death sentence for it, apparently.

Peace,
Neil X.

Edwin Hurwitz

3/19/2011 5:25:00 PM

0

In article
<13e755a0-eb88-4e51-8890-c1ead00c1c9f@w9g2000prg.googlegroups.com>,
Neil X <neilxk@yahoo.com> wrote:

> On Mar 18, 5:41?pm, Gladys <just.c...@say.it.anymore> wrote:
> > On 3/18/2011 2:47 PM, mjd wrote:
> >
> >
> >
> > > Mickey too? ?man I gotta get out more - both drummers gone and I had
> > > no idea!
> >
> > They both got booted for wanting too much money.
>
>
> And were given the death sentence for it, apparently.
>
> Peace,
> Neil X.

They are dead to me. They're nothing to me now. They're not brothers,
They're not friends. I don't want to know them or what they do. I don't
want to see them at the hotels, I don't want them near my house. When
they see Jerry, I want to know a day in advance, so I won't be there.
You understand?

onetaste2000

3/19/2011 5:27:00 PM

0

On Mar 19, 10:25 am, Edwin Hurwitz <ed...@indra.com> wrote:
> In article
> <13e755a0-eb88-4e51-8890-c1ead00c1...@w9g2000prg.googlegroups.com>,
>  Neil X <nei...@yahoo.com> wrote:
>
> > On Mar 18, 5:41 pm, Gladys <just.c...@say.it.anymore> wrote:
> > > On 3/18/2011 2:47 PM, mjd wrote:
>
> > > > Mickey too? man I gotta get out more - both drummers gone and I had
> > > > no idea!
>
> > > They both got booted for wanting too much money.
>
> > And were given the death sentence for it, apparently.
>
> > Peace,
> > Neil X.
>
> They are dead to me. They're nothing to me now. They're not brothers,
> They're not friends. I don't want to know them or what they do. I don't
> want to see them at the hotels, I don't want them near my house. When
> they see Jerry, I want to know a day in advance, so I won't be there.
> You understand?

<stares>