CREATE TABLE XXCUSTOM_XML_TEMPLATES
(
TEMPLATE_ID NUMBER,
XMLSTRUCT CLOB
)
<SCHOOL_ABC>60<CLASS1>30<SECTION1>10</SECTION1><SECTION2>20</SECTION2></CLASS1><CLASS2>15</CLASS2><CLASS3>15</CLASS3></SCHOOL_ABC>
<SCHOOL_$$ABC$$>$$TOTAL$$<CLASS1>$$CLASS1_TOTAL$$<SECTION1>$$SECTION1_TOTAL$$</SECTION1><SECTION2>$$SECTION2_TOTAL$$</SECTION2></CLASS1><CLASS2>$$CLASS2_TOTAL$$</CLASS2><CLASS3>$$CLASS3_TOTAL$$</CLASS3></SCHOOL_$$ABC$$>
SET DEFINE OFF;
Insert into XXCUSTOM_XML_TEMPLATES
(TEMPLATE_ID, XMLSTRUCT)
Values
(1, '<SCHOOL_$$ABC$$>$$TOTAL$$<CLASS1>$$CLASS1_TOTAL$$<SECTION1>$$SECTION1_TOTAL$$</SECTION1><SECTION2>$$SECTION2_TOTAL$$</SECTION2></CLASS1><CLASS2>$$CLASS2_TOTAL$$</CLASS2><CLASS3>$$CLASS3_TOTAL$$</CLASS3></SCHOOL_$$ABC$$>');
COMMIT;
CREATE TABLE XXCUSTOM_XML_TEMPLATE_DTLS
(
TEMPLATE_ID NUMBER,
ELEMENTVAL VARCHAR2(100 BYTE),
ELEMENTQUERY VARCHAR2(100 BYTE)
)
SET DEFINE OFF;
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'TOTAL', 'select 75 from dual');
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'ABC', 'select ''ABC'' from dual');
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'CLASS1_TOTAL', 'select 30 from dual');
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'SECTION1_TOTAL', 'select 10 from dual');
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'SECTION2_TOTAL', 'select 20 from dual');
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'CLASS2_TOTAL', 'select 20 from dual');
Insert into XXCUSTOM_XML_TEMPLATE_DTLS
(TEMPLATE_ID, ELEMENTVAL, ELEMENTQUERY)
Values
(1, 'CLASS3_TOTAL', 'select 25 from dual');
COMMIT;
CREATE TABLE XXCUSTOM_XML_OUTPUT
(
COL1 CLOB
)
CREATE OR REPLACE FUNCTION xxcustom_dyn_xml_generator (
p_template_id IN NUMBER
)
RETURN CLOB
AS
CURSOR c1
IS
SELECT *
FROM xxcustom_xml_template_dtls
WHERE template_id = p_template_id;
v_xml CLOB;
v_res VARCHAR2 (100);
var_sql VARCHAR2 (1000);
PROCEDURE xx_pragma_transaction (p_in_xml CLOB)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE XXCUSTOM_XML_OUTPUT';
INSERT INTO xxcustom_xml_output
VALUES (v_xml);
COMMIT;
END;
BEGIN
SELECT xmlstruct
INTO v_xml
FROM xxcustom_xml_templates
WHERE template_id = p_template_id;
FOR l1 IN c1
LOOP
var_sql := l1.elementquery;
EXECUTE IMMEDIATE var_sql
INTO v_res;
-- DBMS_OUTPUT.put_line (
-- 'var_res for' || l1.elementval || ' is:' || v_res
--);
v_xml := REPLACE (v_xml, '$$' || l1.elementval || '$$', v_res);
END LOOP;
xx_pragma_transaction (v_xml);
RETURN v_xml;
END;
/
CREATE OR REPLACE FUNCTION xxcustom_dyn_xml_generator (
p_template_id IN NUMBER,p_input_var in varchar2
)
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (6)
Author
Commented:Thanks Steve pointing out the typo mistakes.
Had corrected them and also to answer what you had pointed out
(1, 'ABC', 'select ''A B C'' from dual');
This will work fine..
only the problematic special characters are <,>,&,',"
they have to be worked out in select query of the element itself by replacing them with appropriate xml equivalent characters, else we can write a separate function to do this task
taking what you mentioned in comment
(1, 'TOTAL', 'select ''THIS&THAT'' from dual');
Open in new window
after creation
replace the element query with this
select apps.xxcustom_special_char
By this way, these special characters too can be handled.
Would appreciate if you review and suggest if there are any further recommendations.
Commented:
You would have to go through the entire w3 XML spec to make sure your code complies with ALL the rules.
My personal opinion is that the XML functions are far simpler than what you are proposing as easier.
Also, although "<a>60<b>10</b></a>" passes some of the XML validators (Even Oracle will allow it) it is considered by many to be invalid.
The reason: A node should not be a text node and parent node at the same time.
It might even be invalid in the XML spec. I've not read it end-to-end.
>>by replacing them with appropriate xml equivalent characters
Not quite that simple.
Using your original code with your new replace suggestion:
(1, 'ABC', 'select ''ABC'' from dual');
with:
(1, 'ABC', 'select ''THIS&THAT'' from dual');
>>you had pointed out (1, 'ABC', 'select ''A B C'' from dual'); This will work fine..
It generates output but that output isn't valid XML.
Make the change, run it and paste the results into a validator.
>>Would appreciate if you review and suggest if there are any further recommendations.
Sorry for being harsh here but:
My only suggestion would be to rethink this as even a remote possibility of a replacement to the XML functions.
Author
Commented:I just said that a Simple XML can be generated using oracle tables without using any xml functions
>>you had pointed out (1, 'ABC', 'select ''A B C'' from dual'); This will work fine..
To illustrate this, I had showed the content of the xml generator validated via xml.exe utility(in built utility with oracle database generally present under bin folder, and also via jdeveloper)
Open in new window
generated xml is
<SCHOOL_ABC>75<CLASS1>30<S
And if you see the basic logic of how this works, actually its not at all generating any XML, its using a template kind of thing and its just replacing the values which are generated dynamically by using NDS.
So its up to the template to maintain the XML Standard which we insert in the template table.
This kind of approach would be handy(quick to implement and saves considerable development effort) in case we need to integrate with (for suppose) to 100 third party systems where in the middle ware has a limited capability, then a single interface can be built based on this in less time.
Commented:
I stand by what I said.
I would caution anyone reading this article that is dealing with XML: Use the Oracle XML functions and not a bunch of string replacements and call it XML.
Author
Commented:This too is my last comment, to re-iterate that this is just a mechanism to generate xml using tables not anything against oracle xml utilities.
Many people will differ in their thoughts on a same thing, this doesn't make one is correct and other is wrong. They are just different. Same applies here.
View More