Q

Using the XMLElement function

I have a question regarding the XMLElement function. Can the XMLElement tag name be generated dynamically (based on the CASE expression/decode)? I am using the new XSQL capabilities in Oracle 9i, and I failed to do it. This is a very simple case. I would like to know the concept:
SQL> select table_name from user_tables;

TABLE_NAME
------------------------------
DBA_CONSTRAINTS
DBA_INDEXES
DBA_IND_COLUMNS
DBA_LOBS
DBA_SEQUENCES
DBA_SOURCE
DBA_TABLES
DBA_TAB_COLUMNS
DBA_TRIGGERS
DBA_VIEWS
OPCONFIG
PLAN_TABLE

select CASE WHEN table_name='DBA_CONSTRAINTS' THEN xmlelement(name
"DBA_LOBS") ELSE  xmlelement(name "DBA_CONSTRAINTS") END from
user_tables;
And this is what I get:
CASEWHENTABLE_NAME='DBA_CONSTRAINTS'THENXMLELEMENT(NAME"DBA_LOBS")ELSEXMLELEMENT
(NAME"DBA_CONSTRAINTS")END
--------------------------------------------------------------------------------
---------------------------------------------------
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>
<DBA_LOBS></DBA_LOBS>

I honestly am not sure what you're looking for eactly in your query but you asked if you can have dynamic XMLElement generation.

By that, I'm assuming you mean dynamic tags in the output. If so, then yes it is possible. Here is an example using the SCOTT sample user.

SELECT
     CASE table_name
       WHEN 'EMP' THEN
         xmlelement("PrivateInfo", table_name || '')
       WHEN 'BONUS' THEN
         xmlelement("PrivateInfo", table_name )
       ELSE
         xmlelement("PublicInfo", table_name  )
       END xml_frag
  FROM USER_TABLES
  ORDER BY table_name
/

XML_FRAG
---------------------------------------------
<PrivateInfo>BONUS</PrivateInfo>
<PublicInfo>DEPT</PublicInfo>
<PrivateInfo>EMP</PrivateInfo>
<PublicInfo>EMP_AUD</PublicInfo>
<PublicInfo>ORDERS</PublicInfo>
<PublicInfo>PRODUCT_GROUPS</PublicInfo>
<PublicInfo>SALGRADE</PublicInfo>

7 rows selected.
I'm not sure where this feature would be useful, and it seems a little buggy to me. Notice the ||''. I'm researching that. I'm not sure if it's an artifact of the CASE or the XMLElement or a combination. Fascinating. (I'm making Spock eyebrows right now). By the way, I'm running 10g, not 9i.

This was first published in June 2005

Dig deeper on Oracle XML

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close