Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6157723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:49:58+00:00 2026-05-23T20:49:58+00:00

I have xml stored in a table and table with names/paths of the elements

  • 0

I have xml stored in a table and table with names/paths of the elements I am interested in.

I would like to extract values of these elements without hard coding paths and/or names of the elements. Structure of my xml will not change, it will always have parent/child/element.

Is it possible to join xml and table to get to the values of the elements?

Below is example of what I was able to get. I think it is possible to extend this solution to JOIN on ChildNode and Element but not sure how to use .query() and .value() to get to ChildNode and Element.

Thank you for your help.

DECLARE @xml xml
SET @xml = 
'<Products>
    <RedProduct>
      <Details_RedProduct>
        <Width>1</Width>
        <Depth>2</Depth>
        <Weight>3</Weight>
      </Details_RedProduct>
    </RedProduct>
    <GreenProduct>
      <Details_GreenProduct>
        <Width>4</Width>
        <Depth>5</Depth>
        <Height>6</Height>
      </Details_GreenProduct>
      </GreenProduct>
    <BlueProduct>
      <Details_BlueProduct>
        <Width>7</Width>
        <Depth>8</Depth>
        <Lenght>9</Lenght>
      </Details_BlueProduct>
    </BlueProduct>
</Products>'

DECLARE @ProductElement table (ProductNode nvarchar(100), ChildNode nvarchar(100), Element nvarchar(20))
INSERT INTO @ProductElement SELECT 'RedProduct','','Width'
INSERT INTO @ProductElement SELECT 'GreenProduct','','Width'
INSERT INTO @ProductElement SELECT 'GreenProduct','','Height'
UPDATE @ProductElement SET ChildNode = 'Details_' + ProductNode

SELECT ProductsCollection.query('local-name(.)').value('.','nvarchar(100)') as TestOutput
FROM @xml.nodes('//Products/*') productsXml (ProductsCollection)
INNER JOIN @ProductElement el ON el.ProductNode = ProductsCollection.query('local-name(.)').value('.','nvarchar(100)')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:49:59+00:00Added an answer on May 23, 2026 at 8:49 pm

    You can use sql:column() in your xquery expression and compare against local-name(.) to get the nodes you want.

    select PE.ProductNode,
           PE.ChildNode,
           PE.Element,
           T.Col.value('.', 'int') as ElementValue
    from @ProductElement as PE
      cross apply @xml.nodes('/Products/*[local-name(.) = sql:column("PE.ProductNode")]
                                       /*[local-name(.) = sql:column("PE.ChildNode")]
                                       /*[local-name(.) = sql:column("PE.Element")]') as T(Col)
    

    Result:

    ProductNode          ChildNode            Element              ElementValue
    -------------------- -------------------- -------------------- ------------
    RedProduct           Details_RedProduct   Width                1
    GreenProduct         Details_GreenProduct Width                4
    GreenProduct         Details_GreenProduct Height               6
    

    Edit:
    Another version that uses a join on fields instead. It might have a better performance for you depending on what your data looks like. The first version parses the XML for each row in @ProductElement and the second version shreds the XML and uses that to join against @ProductElement.

    select PE.ProductNode,
           PE.ChildNode,
           PE.Element,
           X.ElementValue
    from @ProductElement as PE
      inner join (
                   select T1.Col.value('local-name(.)', 'varchar(100)') as ProductNode,
                          T2.Col.value('local-name(.)', 'varchar(100)') as ChildNode,
                          T3.Col.value('local-name(.)', 'varchar(100)') as Element,
                          T3.Col.value('.',             'varchar(100)') as ElementValue
                   from @xml.nodes('/Products/*') as T1(Col)
                     cross apply T1.Col.nodes('*') as T2(Col)
                     cross apply T2.Col.nodes('*') as T3(Col)
                 ) as X
        on PE.ProductNode = X.ProductNode and
           PE.ChildNode = X.ChildNode and
           PE.Element = X.Element
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have XML files stored in a folder. I would create a table with
I have an xml in which i have stored some html under comments like
I have several xml files, the names of which are stored in another xml
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I have 40 or so stored procedures that i would like to be able
I have some xml data stored in an XML Column in a table in
I have mysql table that has a column that stores xml as a string.
Say I have a table called xml that stores XML files in a single
I have a set of records, stored as XML files, where the XML files
I have a stored procedure which takes an XML parameter and inserts the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.