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>

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: