4

I am not expert of Oracle, but as per requirements I am using Oracle Parser for Parsing Xml. For listed below xml i.e.

<?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>

I have used that method, where l_xmlclob has CLOB datatype and is assigned xml above.

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;

and its working fine and its output is like

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

but the problem arises if there are two values one after the another in an xml and i want to select the first occurrence only i.e. for the below 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>

I want "uganda, AZ12604823-001, IT06686, Hu Mics Metab K, 2006-06-21,2006-07-27" to be selected only within that valueArray Not "Italy and 2012-02-27". But don't don't know how to modify the existing code.

IConfused
  • 714
  • 2
  • 8
  • 20
  • 1
    From your original question i knew "Array" would mean bad things :) It would've helped to have this xml source there, because all that is really needed for a fix is a ´[1]´ somewhere! – Tom Aug 17 '12 at 07:37

1 Answers1

1

I guess this is what you are looking for-

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;

Output-

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] will give you the first of the multi-line element i.e. the first Value singleton.

Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
  • Thanks. Could you please tell me where could I possibly find such related parsing tutorial. – IConfused Aug 17 '12 at 09:43
  • @IConfused there are plenty of tutorials available on the web for this. However the solution I gave is more of a XML XPath related knowledge so you may want to have a look at XML XPath documentation a bit more. Here's and [intriguing document](http://www.oracle.com/technetwork/database/features/xmldb/xmlqueryoptimize11gr2-168036.pdf) on XML DB functions. – Anjan Biswas Aug 17 '12 at 15:22