0

I've got table, which contains XML-type column in it. Some of the values in this column begin with

<MyAuthenticationParams>
.....
</MyAuthenticationParams>

And some with

<Sm1AuthenticationParams>
.......
</Sm1AuthenticationParams>

How can I select only records with parent node MyAuthenticationParams? Thanks for any help.

EDIT: This is how XML looks like

    <MyAuthenticationParams xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <AlsoParams> 
    <SecretKey>MVHXAQA5kF4Ab9siV4vPA4aVPn1EKhbqIBrpCZx2Hg</SecretKey>     <DynamicDescriptor />
  </AlsoParams>
  <myParams>
    <AccountName>Acc1</AccountName>
    <Username>testUsername</Username>
   </myParams>
</MyAuthenticationParams>

or like this

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

  <AccountName>XGwzJ6RR</AccountName>

  <SomeNumber>123456780</SomeNumber>
</Sm1AuthenticationParams>
1
  • 1
    Could you show more of the XML structure? Commented Sep 1, 2017 at 8:23

2 Answers 2

0

You can try this:

DECLARE @DataSource XML =
N'
<DataSource>
<parentA>
    <child1></child1>
    <child2></child2>
    <child3></child3>
</parentA>
<parentB>
    <child4></child4>
    <child5></child5>
    <child6></child6>
</parentB>
<parentA>
    <child7></child7>
    <child8></child8>
</parentA>
</DataSource>';


SELECT P.c.query('.')
      ,T.c.query('.')
FROM @DataSource.nodes('DataSource/parentA') P(c)
OUTER APPLY P.c.nodes('*') T(c);

enter image description here

The idea is to use nodes to get all parentA nodes (in your case these are the MyAuthenticationParams nodes). Then, with second nodes we are getting their children but you can whatever you Want from here.

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

Comments

0
SELECT
a.b.value('NODE_ONE[1]','nvarchar(max)') AS Node_One,
a.b.value('NODE_TWO[1]','nvarchar(max)') AS Node_Two
FROM @MyXML.nodes('XML_MAIN_NODE/MyAuthenticationParams') a(b)

Since your XML is in table, use below.

SELECT
a.b.value('NODE_ONE[1]','nvarchar(max)') AS Node_One,
a.b.value('NODE_TWO[1]','nvarchar(max)') AS Node_Two
FROM Plans s CROSS APPLY s.Params.nodes('XML_MAIN_NODE/MyAuthenticationParams') a(b)

In your case, this works. PATH_TILL_MyAuthenticationParams is the path till the parent node of MyAuthenticationParams tag. So, if XML is like <root><nodeA><MyAuthenticationParams>...<MyAuthenticationParams><Other_tag></Other_tag></nodeA></root>, PATH_TILL_MyAuthenticationParams is 'root/nodeA'

DECLARE @xml XML
select @xml = a.b.query('*') FROM Plans s CROSS APPLY s.Params.nodes('PATH_TILL_MyAuthenticationParams') a(b)
PRINT CONVERT(NVARCHAR(MAX), @xml)

DECLARE @accName NVARCHAR(500)
DECLARE @userName NVARCHAR(500)
DECLARE @secretKey NVARCHAR(2000)
SELECT
@accName = a.b.value('AccountName[1]','nvarchar(max)'),
@userName = a.b.value('Username[1]','nvarchar(max)')
FROM @xml.nodes('MyAuthenticationParams/myParams') a(b)

PRINT @accName
PRINT @userName

SELECT
@secretKey = a.b.value('SecretKey[1]','nvarchar(max)')
FROM @xml.nodes('MyAuthenticationParams/AlsoParams') a(b)
PRINT @secretKey

Edit: In a Single Query,

DECLARE @AccountName NVARCHAR(500) = ''
DECLARE @Username NVARCHAR(500) = ''
DECLARE @Key VARCHAR(500) = ''

    SELECT @AccountName = x.u.value('(/ROOT_NODE/.../MyAuthenticationParams/myParams/AccountName)[1]', 'nvarchar(max)')
        , @Username = x.u.value('(/ROOT_NODE/.../MyAuthenticationParams/myParams/Username)[1]', 'nvarchar(max)'),
        @Key = x.u.value('(/ROOT_NODE/.../MyAuthenticationParams/AlsoParams/SecretKey)[1]', 'nvarchar(max)')
    FROM Plans a  CROSS APPLY a.Params.nodes('ROOT_NODE') x(u)

    PRINT @AccountName
    PRINT @Username
    PRINT @Key

8 Comments

What is a and b in this context?
It's a table valued method. The table (and its columns) returned by a table-valued method need to be aliased. a is for table it creates. c is for column.
I don't quite understand, how can I use it to table. For example, my table name is Plans and column with xml is Params. Should I declare it to vars somwhere?
thank you for that, but it's not working. It returns empty columns
share your xml in the question.
|

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.