Hello, I've just started working with xml and am trying to merge 2 xml documents into a single document.
The XmlAgg query is repeating the some of the data from the first xmltable.
I've created a small example of what I am trying to do (is very basic example)
Select XMLELEMENT("Questionaire",
Xmlagg(
XMLELEMENT("Questions",
Xmlconcat(
Xmlforest(Question_Nbr As "QuestionNumber"
, Question_Type As "QuestionType"
, Question_Asked As "QuestionAsked")
, Xmlelement("QuestionAnswer",Question_Ans)
))Order By Id)) QT
From (Select Qt.Id, Qa.Question_Nbr, Qt.Question_Type, Qt.Question_Asked, Qa.Question_Ans
From Xmltable ('/Test/Questions/question' Passing Xmltype('<Test><Questions><question><type>general</type><asked>Are you ok</asked></question><question><type>specific</type><asked>Do you know why not working</asked></question></Questions></Test>')
COLUMNS
Id For Ordinality
, Question_Type Varchar2(100) Path 'type'
, Question_Asked varchar2(100) path 'asked') Qt
inner join XMLTABLE ('/Results/question' PASSING XMLTYPE('<Results><question><nbr>1</nbr><ans>Yes</ans></question><question><nbr>2</nbr><ans>No</ans></question></Results>')
COLUMNS
Id For Ordinality
, Question_Nbr Number Path 'nbr'
, Question_Ans varchar2(100) path 'ans') Qa
ON Qt.Id = Qa.Id);
produces the following: QuestionNumber is 2 in both Questions/QuestionNumber (more nodes = more repeated values)
<Questionaire>
<Questions>
<QuestionNumber>2</QuestionNumber>
<QuestionType>general</QuestionType>
<QuestionAsked>Are you ok</QuestionAsked>
<QuestionAnswer>Yes</QuestionAnswer>
</Questions>
<Questions>
<QuestionNumber>2</QuestionNumber>
<QuestionType>specific</QuestionType>
<QuestionAsked>Do you know why not working</QuestionAsked>
<QuestionAnswer>No</QuestionAnswer>
</Questions>
</Questionaire>
Running the inner query produces 2 rows with the correct data.
Select Qt.Id, Qa.Question_Nbr, Qt.Question_Type, Qt.Question_Asked, Qa.Question_Ans
From Xmltable ('/Test/Questions/question' Passing Xmltype('<Test><Questions><question><type>general</type><asked>Are you ok</asked></question><question><type>specific</type><asked>Do you know why not working</asked></question></Questions></Test>')
COLUMNS
Id For Ordinality
, Question_Type Varchar2(100) Path 'type'
, Question_Asked varchar2(100) path 'asked') Qt
inner join XMLTABLE ('/Results/question' PASSING XMLTYPE('<Results><question><nbr>1</nbr><ans>Yes</ans></question><question><nbr>2</nbr><ans>No</ans></question></Results>')
COLUMNS
Id For Ordinality
, Question_Nbr Number Path 'nbr'
, Question_Ans varchar2(100) path 'ans') Qa
ON Qt.Id = Qa.Id
QUESTION_NBR | QUESTION_TYPE | QUESTION_ASKED | QUESTION_ANS
1 | general | Are you ok | Yes
2 | specific | Do you know why not working | no
database version:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
"CORE 11.1.0.7.0 Production"
TNS for Linux: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
Can anyone point out where my query has gone wrong.
Thanks
Mike.