简体   繁体   中英

Applying XML cross apply to get data from XML file

I am applying XML cross apply to extract data from XML project file having multiple nodes by using Stored procedure.
Stored Procedure is as follow

INSERT INTO UCDetails
SELECT 
Usecase.value('@UserID','VARCHAR(100)') AS UCId, --ATTRIBUTE 
Usecase.value('@Name','VARCHAR(100)') AS UCName, --ATTRIBUTE 
Usecase.value('@PmAuthor','VARCHAR(100)') AS UCActor, --ATTRIBUTE 
UCPrecon.value('@Value','VARCHAR(MAX)') AS UCPre, --ATTRIBUTE 
UCPostcon.value('@Value','VARCHAR(MAX)') AS UCPost, --ATTRIBUTE 


FROM
@xml.nodes('/Project/Models/UseCase')AS TAB(Usecase) OUTER  
apply @xml.nodes('/Project/Models/UseCase/TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue[5] ')AS TAB1(UCPrecon)OUTER  
apply @xml.nodes('/Project/Models/UseCase/TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue[6]')AS TAB2(UCPostcon)

END

Instead of getting only a single row of data I am getting cross multiplication row as shown below

UCId            UCName           UCActor         UCPre                  UCPost        
      

UC01            Login            Bilal Haider    User must be Registerd User is added   

UC01            Login            Bilal Haider    User is Loggined sucessfully   User is Loggined sucessfully    

UC01            Login            Bilal Haider    User is Loggined sucessfully   User is added   

UC02            Add User         Bilal Haider    User must be Registerd  User is Loggined sucessfully   

UC02            Add User         Bilal Haider    User must be Registerd  User is added  

UC02            Add User         Bilal Haider    User is Loggined sucessfully   User is Loggined sucessfully    

Sorry for posting the result like this Correct rows are highlighted in bold, but Why I am getting all the other rows ?

XML File link: XML FILE

Don't use cross apply against TaggedValues . Specify the xPath in the values clause instead.

select T.X.value('@UserID', 'varchar(100)'),
       T.X.value('@Name', 'varchar(100)'),
       T.X.value('@PmAuthor', 'varchar(100)'),
       T.X.value('(TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue)[5]/@Value', 'varchar(max)'),
       T.X.value('(TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue)[6]/@Value', 'varchar(max)')
from @xml.nodes('/Project/Models/UseCase') as T(X)

Result

------ ---------- ------------- ------------------------------ ------------------------------
UC01   Login      Bilal Haider  User must be Registerd         User is Loggined sucessfully
UC02   Add User   Bilal Haider  User is Loggined sucessfully   User is added

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM