0

I have an XML and I want to get the Xml Node values dynamically. I want to Pass the Node Name and the in return the function or SP should return me the value of that Node.

<ClassificationTypeEntity>

        <LTABLE_ID>3170</LTABLE_ID>

        <LTABLE_CODE>script Code</LTABLE_CODE>

        <LTABLE_DESC>alert(''hello'')</LTABLE_DESC>

        <ACTIVE_YES_NO>1</ACTIVE_YES_NO>enter code here

        <PRIVATE_FILING>0</PRIVATE_FILING>

        <RETENTION_CODE /><RETENTION_TITLE />

        <LTABLE_ID_P>0</LTABLE_ID_P>

</ClassificationTypeEntity>;

For Example, In the above xml, when I want the value of LTABLE_CODE Node, I will pass the same and the result should I get is

script code

Same for LTABLE_DESC the result should be alert(''hello'')

However I can write the 2 Xpath query for both but in case my schema gets changed (more properties added or removed) then I will have to change my SP also.

Thanks, Mohit Jain

2
  • Where's the SQL code you're currently using? Commented Jan 17, 2013 at 13:19
  • As long as your node names aren't changing, you could use //LTABLE_CODE and //LTABLE_DESC Commented Jan 17, 2013 at 14:10

1 Answer 1

1

Thanks for your replies.

I have found the solution:

declare @xmlData xml, @NodeName VarChar(500), @nodeValue VarChar(500)

Set @NodeName = 'LTABLE_CODE';

--Set @NodeName = 'LTABLE_DESC';

set @xmlData = 

 '<ClassificationTypeEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   <LTABLE_ID>3170</LTABLE_ID>

   <LTABLE_CODE>&lt;script&gt;al</LTABLE_CODE>

   <LTABLE_DESC>&lt;script&gt;alert(''hello'')&lt;/script&gt;</LTABLE_DESC>

   <ACTIVE_YES_NO>1</ACTIVE_YES_NO>

   <PRIVATE_FILING>0</PRIVATE_FILING>

   <RETENTION_CODE /><RETENTION_TITLE />

   <LTABLE_ID_P>0</LTABLE_ID_P>

</ClassificationTypeEntity>'

SET @nodeValue = @xmlData.value('(/ClassificationTypeEntity/*[local-name()=sql:variable("@NodeName")])[1]','nvarchar(max)')

SELECT @nodeValue
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.