Obtain node name
595065Sep 5 2007 — edited Sep 11 2007Hi, i wonder if you could please help me with this.
I want to obtain the name of each node in this xml source:
<Objects>
<Label ID="lblTest">Test</Label>
<Label>Another Test</Label>
<Text ID="txtTest">Text Test</Text>
</Objects>
Because i don't want to reference them for its name i was using this xpath to get 'em one by one: /Objects/node()[1], /Objects/node()[2]... and so on.
My question is: is there a expression like node(), attribute(), text(), @name, @node or something like that returns what i want?.
Was trying to get the result i want by processing the node as a string with a function called get_node_name:
DECLARE
lxml_objects xmltype DEFAULT xmltype('
<Objects>
<Label ID="lblTest">Test</Label>
<Label>Another Test</Label>
<Text ID="txtTest">Text Test</Text>
</Objects>
');
lvc2_xpath VARCHAR2(100) DEFAULT '/Objects/node()[:lint_counter]';
lint_counter PLS_INTEGER DEFAULT 1;
-- Function to get the node name
FUNCTION get_node_name(lxml_node IN xmltype) RETURN VARCHAR2 IS
lvc2_node_name VARCHAR2(30) DEFAULT REPLACE(substr(lxml_node.getstringval,1,instr(lxml_node.getstringval,'>')),'<');
lint_end_of_name PLS_INTEGER DEFAULT instr(lvc2_node_name,' ');
BEGIN
IF NOT lint_end_of_name > 0 THEN
lint_end_of_name := length(lvc2_node_name);
END IF;
RETURN substr(lvc2_node_name,1,lint_end_of_name - 1);
END;
BEGIN
WHILE lxml_objects.existsnode(REPLACE(lvc2_xpath,':lint_counter',lint_counter)) = 1
LOOP
dbms_output.put_line('Node ' || to_char(lint_counter) || ' is ' || get_node_name(lxml_objects.extract(REPLACE(lvc2_xpath,':lint_counter',lint_counter))));
lint_counter := lint_counter + 1;
END LOOP;
END;
The results are:
Node 1 is Label
Node 2 is Label
Node 3 is Text
---
Thank you in advance for your help,
Regards.