使用oracle解析器从XML获取First Occurred值

时间:2012-08-16 21:37:24

标签: oracle xml-parsing oracle-apex plsqldeveloper

我不是Oracle的专家,但根据要求,我使用的是Oracle Parser for Parsing Xml。对于下面列出的xml即

<?xml version="1.0" encoding="iso-8859-1" ?>
<SearchOutput>
<rowArray>
    <Row>
        <cellArray>
            <Cell>
                <columnId>1</columnId>
                <valueArray>
                    <Value>
                        <value>IR000024575453</value>
                    </Value>
                </valueArray>
            </Cell>
            <Cell>
                <columnId>5</columnId>
                <valueArray>
                    <Value>
                        <value>AZ12604823-001</value>
                    </Value>
                </valueArray>
            </Cell>
            <Cell>
                <columnId>2</columnId>
                <valueArray>
                    <Value>
                        <value>IT06686</value>
                    </Value>
                </valueArray>
            </Cell>
            <Cell>
                <columnId>9</columnId>
                <valueArray>
                    <Value>
                        <value>Hu Mics Metab K</value>
                    </Value>
                </valueArray>
            </Cell>
            <Cell>
                <columnId>8</columnId>
                <valueArray>
                    <Value>
                        <value>2006-06-21</value>
                    </Value>
                </valueArray>
            </Cell>
            <Cell>
                <columnId>7</columnId>
                <valueArray>
                    <Value>
                        <value>2006-07-27</value>
                    </Value>
                </valueArray>
            </Cell>
        </cellArray>
    </Row>
</rowArray>
</SearchOutput>

我使用过该方法,其中l_xmlclob具有CLOB数据类型,并在上面指定了xml。

FOR r IN (  SELECT rownum rn, cells
           FROM xmltable('/SearchOutput/rowArray/Row' passing XMLTYPE(l_xmlclob)
                                     columns CELLS  XMLTYPE PATH './cellArray')
        )
 LOOP
  DBMS_OUTPUT.PUT_LINE('Row: '||r.rn);      

  FOR c IN ( SELECT colid, colval
               FROM xmltable('/cellArray/Cell' passing r.cells
                                               columns COLID NUMBER PATH './columnId',
                                                       COLVAL VARCHAR(20) PATH     './valueArray/Value/value')
           )
  LOOP
     DBMS_OUTPUT.PUT_LINE('colid, col value: '||c.colid||', '||c.colval);
  END LOOP;
 END LOOP;

它的工作正常,其输出就像

Row: 1
colid, col value: 1, IR000024575453
colid, col value: 5, AZ12604823-001
colid, col value: 2, IT06686
colid, col value: 9, Hu Mics Metab K
colid, col value: 8, 2006-06-21
colid, col value: 7, 2006-07-27

但是如果在xml中一个接一个地存在两个值并且我想仅选择第一个匹配项,即下面的xml

,则会出现问题
<?xml version="1.0" encoding="iso-8859-1" ?>
<SearchOutput>
<rowArray>
<Row>
    <cellArray>
        <Cell>
            <columnId>1</columnId>
            <valueArray>
                <Value>
                    <value>Uganda</value>
                </Value>
                <Value>
                    <value>Italy</value>
                </Value>
            </valueArray>
        </Cell>
        <Cell>
            <columnId>5</columnId>
            <valueArray>
                <Value>
                    <value>AZ12604823-001</value>
                </Value>
            </valueArray>
        </Cell>
        <Cell>
            <columnId>2</columnId>
            <valueArray>
                <Value>
                    <value>IT06686</value>
                </Value>
            </valueArray>
        </Cell>
        <Cell>
            <columnId>9</columnId>
            <valueArray>
                <Value>
                    <value>Hu Mics Metab K</value>
                </Value>
            </valueArray>
        </Cell>
        <Cell>
            <columnId>8</columnId>
            <valueArray>
                <Value>
                    <value>2006-06-21</value>
                </Value>
            </valueArray>
        </Cell>
        <Cell>
            <columnId>7</columnId>
            <valueArray>
                <Value>
                    <value>2006-07-27</value>
                </Value>
                <Value>
                    <value>2012-02-27</value>
                </Value>
            </valueArray>
        </Cell>
    </cellArray>
</Row>
</rowArray>
</SearchOutput>

我希望“uganda,AZ12604823-001,IT06686,Hu Mics Metab K,2006-06-21,2006-07-27”仅在该值内选择不是“意大利和2012-02-27”。但不要不知道如何修改现有代码。

1 个答案:

答案 0 :(得分:1)

我想这就是你要找的 -

FOR r IN (  SELECT rownum rn, cells
           FROM xmltable('/SearchOutput/rowArray/Row' passing XMLTYPE(l_xmlclob)
                                     columns CELLS  XMLTYPE PATH './cellArray') f
        )
 LOOP
  DBMS_OUTPUT.PUT_LINE('Row: '||r.rn);      
  FOR c IN ( SELECT colid, 
                    colval
               FROM xmltable('for $i in /cellArray/Cell
                              return $i' 
                              passing r.cells
                              columns COLID NUMBER PATH 'columnId',
                                      COLVAL VARCHAR(20) PATH 'valueArray/Value[1]/value')                  
           )
  LOOP
     DBMS_OUTPUT.PUT_LINE('colid, col value: '||c.colid||', '||c.colval);
  END LOOP;
 END LOOP;
end;

OR

FOR r IN (  SELECT rownum rn, cells
           FROM xmltable('/SearchOutput/rowArray/Row' passing XMLTYPE(l_xmlclob)
                                     columns CELLS  XMLTYPE PATH './cellArray')
        )
 LOOP
  DBMS_OUTPUT.PUT_LINE('Row: '||r.rn);      

  FOR c IN ( SELECT colid, colval
               FROM xmltable('/cellArray/Cell' passing r.cells
                                               columns COLID NUMBER PATH './columnId',
                                                       COLVAL VARCHAR(20) PATH     './valueArray/Value[1]/value')
           )
  LOOP
     DBMS_OUTPUT.PUT_LINE('colid, col value: '||c.colid||', '||c.colval);
  END LOOP;
 END LOOP;

输出 -

Row: 1
colid, col value: 1, Uganda
colid, col value: 5, AZ12604823-001
colid, col value: 2, IT06686
colid, col value: 9, Hu Mics Metab K
colid, col value: 8, 2006-06-21
colid, col value: 7, 2006-07-27

Value[1]将为您提供第一个多行元素,即第一个Value单例。