How to get first element by XPath in Oracle
Asked Answered
F

1

8

In my Oracle db I have records like this one:

<ROOT>
  <Event>
    <Type>sldkfvjhkljh</Type>
    <ID>591252</ID>
  </Event>
  <Data>
    <File>
      <Name>1418688.pdf</Name>
      <URL>/591252/1418688.pdf</URL>
    </File>
    <File>
      <Name>1418688.xml</Name>
      <URL>/591252/1418688.xml</URL>
    </File>
  </Data>
</ROOT>

I need to extract a value from the first <Name> tag. If I try:

Select xmltype(xml_data).extract('//Name[1]/text()').getStringVal() from MY_TABLE

I get:

1418688.pdf1418688.xml

Why is that and how can I get just 1418688.pdf?

Oracle Version:

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

Foxing answered 8/7, 2010 at 4:58 Comment(1)
Just wondering, what do you get from xpath //File[1]/Name/text() ? I think that both Name elements are #1, because in their nodes they are each first.Maiolica
M
13

I think that both Name elements are #1 in this doc, because in their nodes they are each first. Try //File[1]/Name/text()

Maiolica answered 8/7, 2010 at 5:26 Comment(1)
//File[last()]/Name/text() should work in that case.Maiolica

© 2022 - 2024 — McMap. All rights reserved.