在Oracle SQL中选择xpath值作为单独的行。

时间:2022-05-09 22:51:41

I need to select some values from an XML stored in a CLOB column in an Oracle database. The best I could come up with is the following:

我需要从存储在Oracle数据库CLOB列中的XML中选择一些值。我能想到的最好的办法是:

select extract(xmltype(COLUMN), 'xpath-expression').getStringVal() as XMLVAL from TABLE t;

The problem is that when the XPATH selects multiple nodes, the values are concatenated. I need to have each selected node on a separate row. Obviously the concatenation must occur in getStringVal(), I use that because I need to have strings in my client (not XMLType). What should I use instead of getStringVal()?

问题是,当XPATH选择多个节点时,这些值会被连接起来。我需要在单独的行上有每个选定的节点。显然,连接必须发生在getStringVal()中,我使用它是因为我需要在我的客户机中有字符串(不是XMLType)。我应该使用什么来代替getStringVal()?

EDIT: note that there is a similar question here: Oracle Pl/SQL: Loop through XMLTYPE nodes - but I couldn't apply it to my case. It uses two different XPATH expressions, and the principle of separation is not clear.

编辑:注意这里有一个类似的问题:Oracle Pl/SQL:循环通过XMLTYPE节点——但是我不能将它应用到我的案例中。它使用了两个不同的XPATH表达式,分离的原则也不清楚。

EDIT2: The XML is very complex, but basically I need to find the "some value" entries in

EDIT2: XML非常复杂,但基本上我需要找到“一些值”条目

<string name="SOME_KEY" value="some value"/>

elements that are burried under many other elements. I use the XPATH //*[@name="SOME_KEY"]/@value and it finds successfully the value attribute of all the XML elements that have a SOME_KEY attribute.

隐藏在许多其他元素之下的元素。我使用XPATH //*[@name="SOME_KEY"]/@value,它成功地找到具有SOME_KEY属性的所有XML元素的值属性。

2 个解决方案

#1


3  

Try this.

试试这个。

SELECT EXTRACTVALUE (x.COLUMN_VALUE, 'xpath-expression')
  FROM TABLE (
          SELECT XMLSEQUENCE (
                    xmltype (column).EXTRACT ('xpath-expression'))
            FROM t) x;

Sample at http://sqlfiddle.com/#!4/87af2/1

示例:http://sqlfiddle.com/ # ! 4/87af2/1

#2


1  

I had close to the same thing, but it didn't quite work with "Eat A Peach"'s answer. I had something like the following in a column as xmltype.

我也有过类似的经历,但这并不完全适用于“吃桃子”的回答。我在列中有如下内容:xmltype。

<?xml version="1.0" encoding="UTF-8"?>
<serviceRequestAnswer xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns2="http://www.something.something/bla/v1">
  <Persons>
    <Person>
      <InternalIdNumber>2935612467</InternalIdNumber>
      <PublicIdNumber>9871256327</PublicIdNumber>
      <FirstNames>Remy</FirstNames>
      <LastName>Smith</LastName>
      <BirthName>Smith</BirthName>
      <BirthDate>19900101</BirthDate>
      <PlaceOfBirth>0209</PlaceOfBirth>
      <CountryOfBirth>6030</CountryOfBirth>
      <Sex>M</Sex>
      <Nationality>0001</Nationality>
    </Person>
    <Person>
      <InternalIdNumber>7163584061</InternalIdNumber>
      <PublicIdNumber>123432678</PublicIdNumber>
      <FirstNames>Jesse</FirstNames>
      <LastName>Smith</LastName>
      <BirthName>Smith</BirthName>
      <BirthDate>19900101</BirthDate>
      <PlaceOfBirth>0012</PlaceOfBirth>
      <CountryOfBirth>6030</CountryOfBirth>
      <Sex>M</Sex>
      <Nationality>0001</Nationality>
    </Person>
  </Persons>
</serviceRequestAnswer>

Let's call the column xmlcontent, and have this in a table named mytable. Then extracting the 2 public ID numbers as 2 rows can be done like so:

让我们调用列xmlcontent,并将其放在名为mytable的表中。然后将2个公共ID号提取为2行,如下所示:

select  
nvl(value (line).extract ('/Person/PublicIdNumber/text()').getstringval (),'') PublicId
from mytable, table ( xmlsequence (extract(xmlcontent,'serviceRequestAnswer/Persons/Person'))) line 
where id_mytable = 10092053;

Hope this helps someone :)

希望这能帮助某人:

#1


3  

Try this.

试试这个。

SELECT EXTRACTVALUE (x.COLUMN_VALUE, 'xpath-expression')
  FROM TABLE (
          SELECT XMLSEQUENCE (
                    xmltype (column).EXTRACT ('xpath-expression'))
            FROM t) x;

Sample at http://sqlfiddle.com/#!4/87af2/1

示例:http://sqlfiddle.com/ # ! 4/87af2/1

#2


1  

I had close to the same thing, but it didn't quite work with "Eat A Peach"'s answer. I had something like the following in a column as xmltype.

我也有过类似的经历,但这并不完全适用于“吃桃子”的回答。我在列中有如下内容:xmltype。

<?xml version="1.0" encoding="UTF-8"?>
<serviceRequestAnswer xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns2="http://www.something.something/bla/v1">
  <Persons>
    <Person>
      <InternalIdNumber>2935612467</InternalIdNumber>
      <PublicIdNumber>9871256327</PublicIdNumber>
      <FirstNames>Remy</FirstNames>
      <LastName>Smith</LastName>
      <BirthName>Smith</BirthName>
      <BirthDate>19900101</BirthDate>
      <PlaceOfBirth>0209</PlaceOfBirth>
      <CountryOfBirth>6030</CountryOfBirth>
      <Sex>M</Sex>
      <Nationality>0001</Nationality>
    </Person>
    <Person>
      <InternalIdNumber>7163584061</InternalIdNumber>
      <PublicIdNumber>123432678</PublicIdNumber>
      <FirstNames>Jesse</FirstNames>
      <LastName>Smith</LastName>
      <BirthName>Smith</BirthName>
      <BirthDate>19900101</BirthDate>
      <PlaceOfBirth>0012</PlaceOfBirth>
      <CountryOfBirth>6030</CountryOfBirth>
      <Sex>M</Sex>
      <Nationality>0001</Nationality>
    </Person>
  </Persons>
</serviceRequestAnswer>

Let's call the column xmlcontent, and have this in a table named mytable. Then extracting the 2 public ID numbers as 2 rows can be done like so:

让我们调用列xmlcontent,并将其放在名为mytable的表中。然后将2个公共ID号提取为2行,如下所示:

select  
nvl(value (line).extract ('/Person/PublicIdNumber/text()').getstringval (),'') PublicId
from mytable, table ( xmlsequence (extract(xmlcontent,'serviceRequestAnswer/Persons/Person'))) line 
where id_mytable = 10092053;

Hope this helps someone :)

希望这能帮助某人: