SQLX - XMLForest [message #129195] |
Fri, 22 July 2005 07:24 |
marrec
Messages: 35 Registered: May 2005 Location: Mühlheim, Germany
|
Member |
|
|
hi there,
i have a SQLX Question:
I've got a table with three cols:
Parameter_Name, Parameter_Value, Parameter_Type
I'm supposed to get an output like this:
<parameter_name parameter_type=XYZ>parameter_value</parameter_name>
f.i.
<name type=varchar2>Marc</name>
<age type=number>28</age>
<dob type=date>08.12.1976</dob>
i tried this by using xmlelement and xmlforest, but it didn't work. i just can't use the values of one column as a name of the tag.
any suggestions?
thx,
marrec
|
|
|
Re: SQLX - XMLForest [message #130837 is a reply to message #129195] |
Tue, 02 August 2005 17:12 |
mchadder
Messages: 224 Registered: May 2005 Location: UK
|
Senior Member |
|
|
Hello,
sorry i'm a bit late on answering this one, but if you still haven't sorted this one from elsewhere, then you're right in that you can't "dynamically" modify the name of the XML element, since
it's an identifier, not a string, the only way would be to pass the values into a PL/SQL function which returned an XMLTYPE and XMLAGG the lot together, i.e.
SQL> CREATE TABLE elements ( parameter_name VARCHAR2(10),
2 parameter_type VARCHAR2(10),
3 parameter_value VARCHAR2(10) )
4 /
Table created.
SQL> INSERT INTO elements VALUES ( 'ELEMENT1', 'TYPE1', 'VALUE1' )
2 /
1 row created.
SQL>
SQL> INSERT INTO elements VALUES ( 'ELEMENT2', 'TYPE2', 'VALUE2' )
2 /
1 row created.
SQL> CREATE OR REPLACE FUNCTION get_xml ( p_name IN VARCHAR2,
2 p_type IN VARCHAR2,
3 p_value IN VARCHAR2 )
4 RETURN XMLTYPE
5 AS
6 x XMLTYPE;
7 BEGIN
8 EXECUTE IMMEDIATE
9 'SELECT XMLELEMENT("' || p_name || '", ' ||
10 'XMLATTRIBUTES(:1 "type"), :2) FROM dual'
11 INTO x
12 USING p_type, p_value;
13 RETURN x;
14 END get_xml;
15 /
Function created.
SQL> SELECT
2 XMLAGG(get_xml(e.parameter_name, e.parameter_type, e.parameter_value))
3 FROM
4 elements e
5 /
XMLAGG(GET_XML(E.PARAMETER_NAME,E.PARAMETER_TYPE,E.PARAMETER_VALUE))
----------------------------------------------------------------------------------------
<ELEMENT1 type="TYPE1">VALUE1</ELEMENT1><ELEMENT2 type="TYPE2">VALUE2</ELEMENT2>
1 row selected.
|
|
|