Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Using the XMLElement function
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Using the XMLElement function

Lewis Cunningham EXPERT RESPONSE FROM: Lewis Cunningham

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 03 June 2005
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.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Oracle XML
Oracle XML DB generates power for electrical agency
Oracle updates Microsoft developer tools
Oracle's content management software plan takes shape
Top five data management buzzwords
Oracle updates free Web development tool
Oracle open sources TopLink at EclipseCon
Oracle or SQL Server for developing social networking site?
What is SOA?
Calling Web service from Oracle vs. .Net
Introduction to BPEL

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
DSML  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts