Oracle SQL

    Tag Archives: xmlnamespace

    Bug with xmltable, xmlnamespaces and xquery_string specified using bind variable

    Posted on July 24, 2014 by Sayan Malakshinov Posted in bug, oracle 3 Comments

    Today I was asked about strange problem: xmltable does not return data, if xquery specified by bind variable and xml data has xmlnamespaces:

    SQL> var x_path varchar2(100);
    SQL> var x_xml  varchar2(4000);
    SQL> col x format a100;
    SQL> begin
      2      :x_path:='/table/tr/td';
      3      :x_xml :=q'[
      4                  <table xmlns="http://www.w3.org/tr/html4/">
      5                    <tr>
      6                      <td>apples</td>
      7                      <td>bananas</td>
      8                    </tr>
      9                  </table>
     10                  ]';
     11  end;
     12  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select
      2        i, x
      3   from xmltable( xmlnamespaces(default 'http://www.w3.org/tr/html4/'),
      4                  :x_path -- bind variable
      5                  --'/table/tr/td' -- same value as in the variable "X_PATH"
      6                  passing xmltype(:x_xml)
      7                  columns i    for ordinality,
      8                          x    xmltype path '.'
      9                );
    
    no rows selected
    

    But if we comment bind variable and comment out literal x_query ‘/table/tr/td’, query will return data:

    SQL> select
      2        i, x
      3   from xmltable( xmlnamespaces(default 'http://www.w3.org/tr/html4/'),
      4                  --:x_path -- bind variable
      5                  '/table/tr/td' -- same value as in the variable "X_PATH"
      6                  passing xmltype(:x_xml)
      7                  columns i    for ordinality,
      8                          x    xmltype path '.'
      9                );
    
             I X
    ---------- -------------------------------------------------------------------
             1 <td xmlns="http://www.w3.org/tr/html4/">apples</td>
             2 <td xmlns="http://www.w3.org/tr/html4/">bananas</td>
    
    2 rows selected.
    

    The only workaround I found is the specifying any namespace in the x_query – ‘/*:table/*:tr/*:td’

    SQL> exec :x_path:='/*:table/*:tr/*:td'
    
    PL/SQL procedure successfully completed.
    
    SQL> select
      2        i, x
      3   from xmltable( xmlnamespaces(default 'http://www.w3.org/tr/html4/'),
      4                  :x_path -- bind variable
      5                  passing xmltype(:x_xml)
      6                  columns i    for ordinality,
      7                          x    xmltype path '.'
      8                );
    
             I X
    ---------- -------------------------------------------------------------------
             1 <td xmlns="http://www.w3.org/tr/html4/">apples</td>
             2 <td xmlns="http://www.w3.org/tr/html4/">bananas</td>
    
    2 rows selected.
    

    It’s quite ugly solution, but I’m not sure whether there is another solution…

    bind variable xmlnamespace xmltable xpath xquery

    Simple Android Oracle client

    Get it on Google Play

    About us

    photo Sayan Malakshinov

    Oracle ACE Associate Oracle ACE Associate
    Oracle performance tuning expert
    Russia / Moscow / Transmedia Dynamics
    photo Bair Malakshinov

    Ph.d candidate
    Senior Oracle Developer
    Poland / Cracow / Luxoft

    Popular Posts

    • Differences between integer(int) in SQL and PL/SQL 0 comments
    • Deterministic function vs scalar subquery caching. Part 1 8 comments
    • Amazing optimization of getting distinct values from the index, and TopN for each of them 4 comments
    • SQL*Plus tips #6: Colorizing output 4 comments
    • SQL*Plus tips #5: sql_text/sql_fulltext formatting(sql beatifier) 13 comments
    • SQL*Plus tips. #1 5 comments
    • A couple of well-known but often forgotten things for PL/SQL developers 2 comments
    • SYS_OP_MAP_NONNULL is in the documentation now 0 comments
    • SQL*Plus tips #4: Branching execution 0 comments
    • Oracle 12c: Lateral, row_limiting_clause 3 comments

    Recent Posts

    • Top-N again: fetch first N rows only vs rownum
    • Docker with Oracle database: install patches automatically
    • Top N biggest tables (with lobs, indexes and nested table)
    • “Collection iterator pickler fetch”: pipelined vs simple table functions
    • SQL*Plus tips #8: How to read the output of dbms_output without “serveroutput on”

    Email Subscription

    Recent Comments

    • SQL*Plus 256 Colours in terminal | EDUARDO CLARO on SQL*Plus tips #6: Colorizing output
    • A simple SQL*Plus parameter parser | EDUARDO CLARO on SQL*Plus tips. #1
    • Sayan Malakshinov on “Collection iterator pickler fetch”: pipelined vs simple table functions
    • Frits on “Collection iterator pickler fetch”: pipelined vs simple table functions
    • SQL*Plus tips #8: How to read the output of dbms_output without 'serveroutput on' - SSWUG.ORG on SQL*Plus tips #8: How to read the output of dbms_output without “serveroutput on”
    • Adaptive serial direct path read decision ignores object statistics since 12.1 - SSWUG.ORG on Adaptive serial direct path read decision ignores object statistics since 12.1
    • Oracle issues after upgrade to 12.2 - SSWUG.ORG on Oracle issues after upgrade to 12.2
    • Ampersand instead of colon for bind variables - SSWUG.ORG on Ampersand instead of colon for bind variables
    • Евгений Бабин on Oracle issues after upgrade to 12.2
    • Oracle SQL | How even empty trigger increases redo generation on Triggers and Redo: changes on 12.2

    Blogroll

    • Alex Fatkulin
    • Alexander Anokhin
    • Andrey Nikolaev
    • Charles Hooper
    • Christian Antognini
    • Coskan Gundogar
    • David Fitzjarrell
    • Igor Usoltsev
    • Jonathan Lewis
    • Karl Arao
    • Mark Bobak
    • Martin Bach
    • Martin Berger
    • Neil Chandler
    • Randolf Geist
    • Richard Foote
    • Riyaj Shamsudeen
    • Tanel Poder
    • Timur Akhmadeev
    • Valentin Nikotin

    Categories

    Aggregated by OraNA Aggregated by OraFAQ

    Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    ©Sayan Malakshinov. Oracle SQL