由于在另一个重复模式中重复节点,因此在PL / SQL中提取部分XMLType

时间:2013-08-26 13:20:33

标签: sql xml oracle plsql xmltype


我有一个XMLType对象,我想将开放时间提取到表中。

<workspace>
  <title>workspace1</title>
  <item>
    <day>1</day>
    <openingTime>8:00</openingTime>
    <closingTime>12:00</closingTime>
  </item>
  <item>
    <day>1</day>
    <openingTime>13:00</openingTime>
    <closingTime>18:00</closingTime>
  </item>
<workspace>
<workspace>
  <title>workspace2</title>
  <item>
    <day>1</day>
    <openingTime>9:00</openingTime>
    <closingTime>14:00</closingTime>
  </item>
  <item>
    <day>3</day>
    <openingTime>12:00</openingTime>
    <closingTime>16:00</closingTime>
  </item>
<workspace>

我会使用类似的东西:

SELECT ExtractValue(Value(p),'workspace/item/day/text()') as day
      ,ExtractValue(Value(p),'workspace/item/openingTime/text()') as open
      ,ExtractValue(Value(p),'workspace/item/closingTime/text()') as close
      FROM TABLE (XMLSequence(Extract(y,'workspace'))) p
      WHERE ExtractValue(Value(p),'/workspace/title/text()') LIKE 'workspace1';

其中 y 是上面的XMLType。但这不会起作用,因为它仍会找到多个节点。我需要提取标题workspace2的所有元素值(值1,9,9:00,14:00,3:00,12:00)。如果我不仅可以提取值,而且可以提取XMLType的整个部分,这将有所帮助。有任何想法吗?
谢谢,Michal

2 个答案:

答案 0 :(得分:2)

您的目标可以通过XMLTable

来实现
with x as ( -- Just to introduce XML parameter
  select 
    xmltype('
       <workspace_list>     
        <workspace>
          <title>workspace1</title>
          <item>
            <day>1</day>
            <openingTime>8:00</openingTime>
            <closingTime>12:00</closingTime>
          </item>
          <item>
            <day>1</day>
            <openingTime>13:00</openingTime>
            <closingTime>18:00</closingTime>
          </item>
        </workspace>
        <workspace>
          <title>workspace2</title>
          <item>
            <day>1</day>
            <openingTime>9:00</openingTime>
            <closingTime>14:00</closingTime>
          </item>
          <item>
            <day>3</day>
            <openingTime>12:00</openingTime>
            <closingTime>16:00</closingTime>
          </item>
        </workspace>
       </workspace_list>     
  ') xfield
  from dual
)  
select 
  workspace, day, opening_time, closing_time
from
 XMLTable(
   '
     for $i in $doc//workspace[title eq $workspace_filter]
     for $j in $i/item 
     return 
      <wks_item>
        <wks_name>{$i/title/text()}</wks_name> 
        {$j/*}
      </wks_item> 
   '
   passing 
    (select xfield from x) as "doc",
    ('workspace1')         as "workspace_filter"
   columns
   workspace     varchar2(100) path '//wks_name',
   day           varchar2(100) path '//day',
   opening_time  varchar2(100) path '//openingTime',
   closing_time  varchar2(100) path '//closingTime'
 )

SQLFiddle Example

请注意,我引入了<workspace_list> top元素并添加了斜杠来关闭<workspace>元素以使XML有效。

答案 1 :(得分:1)

使用某些@ThinkJet技巧,您的查询可能如下所示

with x as (
  select 
    xmltype('
       <workplaces>     
        <workspace>
          <title>workspace1</title>
          <item>
            <day>1</day>
            <openingTime>8:00</openingTime>
            <closingTime>12:00</closingTime>
          </item>
          <item>
            <day>1</day>
            <openingTime>13:00</openingTime>
            <closingTime>18:00</closingTime>
          </item>
        </workspace>
        <workspace>
          <title>workspace2</title>
          <item>
            <day>1</day>
            <openingTime>9:00</openingTime>
            <closingTime>14:00</closingTime>
          </item>
          <item>
            <day>3</day>
            <openingTime>12:00</openingTime>
            <closingTime>16:00</closingTime>
          </item>
        </workspace>
       </workplaces>     
  ') xfield
  from dual
)
SELECT "day", "openingTime", "closingTime"
FROM xmltable('$doc//workspace[title=$workspace_filter]/item'
              passing (select xfield from x) as "doc",
                      ('workspace1') as "workspace_filter"
               columns "openingTime" path '//openingTime',
                       "closingTime" path '//closingTime',
                       "day" path '//day')