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 8623649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:20:04+00:00 2026-06-12T07:20:04+00:00

How can the following statement best be modified so that I receive a list

  • 0

How can the following statement best be modified so that I receive a list of Contact.Name with Address.Location (I’d like to use XPath and not subqueries/WHERE-conditions if possible)? I’ve tried all sorts of syntax at @@@Name but no luck 🙁 – I want to reference the original node there.

DECLARE @data AS XML
SELECT @data = '
    <Data>
        <Contact Name="John"/>
        <Contact Name="Bob"/>
        <Address ContactName="John" Location="JohnStreet1"/>
        <Address ContactName="John" Location="JohnStreet2"/>
        <Address ContactName="Bob" Location="BobStreet1"/>
    </Data> 
'

SELECT 
    x.v.value('@Name','VARCHAR(255)') [Contact.Name],   
    y.v.value('@Location','VARCHAR(255)') [Address.Location]
FROM 
    @data.nodes('/Data[1]/Contact') AS x(v)
CROSS APPLY
    x.v.nodes('/Data[1]/Address[@ContactName=@@@Name]') AS y(v)
ORDER BY
    x.v.value('@Name','VARCHAR(255)')

PS. This won’t work since the parameter needs to be a literal 🙁

x.v.nodes('/Data[1]/Address[@ContactName=' + x.v.value('@Name','VARCHAR(255)') + ']') AS y(v)

Please note: Currently I’m doing the following to get the desired resultset (but not in a clean XPath way):

SELECT 
    x.v.value('@Name','VARCHAR(255)') [Contact.Name],   
    y.v.value('@Location','VARCHAR(255)') [Address.Location]
FROM 
    @data.nodes('/Data[1]/Contact') AS x(v)
CROSS APPLY
    x.v.nodes('/Data[1]/Address') AS y(v)
WHERE
    y.v.value('@ContactName','VARCHAR(255)')=x.v.value('@Name','VARCHAR(255)')
ORDER BY
    x.v.value('@Name','VARCHAR(255)')
  • 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-06-12T07:20:05+00:00Added an answer on June 12, 2026 at 7:20 am

    Here is a pure XQuery (which also happens to be a pure XPath 2.0 expression):

      for $c in /*/Contact/@Name
        return
          for $loc in /*/Address[@ContactName eq $c]/@Location
               return
                  data(($c, $loc, '&#xA;'))
    

    When this XPath expression is evaluated against the provided XML document:

    <Data>
        <Contact Name="John"/>
        <Contact Name="Bob"/>
        <Address ContactName="John" Location="JohnStreet1"/>
        <Address ContactName="John" Location="JohnStreet2"/>
        <Address ContactName="Bob" Location="BobStreet1"/>
    </Data>
    

    the wanted, correct result is produced:

    John JohnStreet1
     John JohnStreet2 
     Bob BobStreet1 
    

    Do Note:

    This is a pure XQuery / XPath 2.0 solution. I don’t know the SQL Server XQuery dialect and hope that you would be able to apply this solution to your case.

    Probably something like:

    select @data.query(
          'for $c in /*/Contact/@Name
            return
              for $loc in /*/Address[@ContactName eq $c]/@Location
                   return
                      data(($c, $loc, "&#xA;"))'
                       )
    

    Update:

    As noted in a comment by Mikael Eriksson, SQL Server XQuery doesn’t accept “heterogeneous sequences” and raises an error.

    Then this, slightly modified query works:

    select @data.query(
          'for $c in /*/Contact/@Name
            return
              for $loc in /*/Address[@ContactName eq $c]/@Location
                   return
                      (string($c), string($loc), "&#xA;")'
    

    and produces the above wanted, correct result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't believe that the following statement seems to be still true So, I
I currently have the following update statement but is there anyway that I can
How can you write the following statement in the given languages? a(0) = 1
Can you please point out what I am doing wrong in following statement? $db
Can I have a prepared statement with the following query: select * from table
Can anyone please decode the following nested IIF to a CASE statement in SQL..
How can i match the following dates with python regular expression in one statement,
In the following code how can I still have the second and third statement
How can the following code be modified to run my insert statements 100 at
Given that Groovy does not have a do-while statement, how can I iterate over

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.