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

  • Home
  • SEARCH
  • 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 415713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:24:30+00:00 2026-05-12T18:24:30+00:00

Given the following sample of XML and the select statement that shreds the xml

  • 0

Given the following sample of XML and the select statement that shreds the xml into a relation, what I need is the second column of the select to be the ordinal of the category (ie 1 for the directions and 2 for the colours in this case).

Note: The literal value ‘rank()’ in the select is left a placeholder. I was poking around with using the rank, but with no success.

declare @x xml
set @x = '
    <root>
        <category>
            <item value="north"/>
            <item value="south"/>
            <item value="east"/>
            <item value="west"/>
        </category>
        <category>
            <item value="red"/>
            <item value="green"/>
            <item value="blue"/>
        </category>
    </root>'

select c.value('./@value', 'varchar(10)') as "ItemValue", 
       'rank()' as "CategoryNumber"
from @x.nodes('//item') as t(c)
  • 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-12T18:24:30+00:00Added an answer on May 12, 2026 at 6:24 pm

    Jacob Sebastian also has an interesting solution presented in his blog post:

    XQuery Lab 23 – Retrieving values and position of elements

    With Jacob’s suggestion, I can rewrite your query to be:

    SELECT
        x.value('@value','VARCHAR(10)') AS 'ItemValue',        
        p.number as 'CategoryNumber'
    FROM
        master..spt_values p
    CROSS APPLY 
        @x.nodes('/root/category[position()=sql:column("number")]/item') n(x) 
    WHERE
        p.type = 'p'
    

    and I get the desired output:

    ItemValue   CategoryNumber
    ---------   --------------
    north           1
    south           1
    east            1
    west            1
    red             2
    green           2
    blue            2
    

    Unfortunately, none of the more obvious solutions like the position() or fn:id() functions seem to a) work in SQL Server or b) be supported in SQL Server at all 🙁

    Hope this helps

    Marc

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

Sidebar

Related Questions

I have written the following piece of code that reads through a given xml-file
Given some sample data.xml file: <?xml version=1.0 encoding=utf-8?> <data> <categories> <category id=google> <name>Google</name> </category>
Given the following sample XML (here I am showing hard-coded, but normally I load
Given the following code sample: uint8_t i, in, ni; i = in = 2;
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
I've an xml file (Sample.xml) which has the following structure <RootElement> <Children> <Child Name=FirstChild
Given the following XML file: <?xml version=1.0 encoding=UTF-8?> <process name=TestSVG2 xmlns=http://www.example.org targetNamespace=http://www.example.org xmlns:xsd=http://www.w3.org/2001/XMLSchema> <sequence>
I've an xml file (Sample.xml) which has the following structure <RootElement> <Child Name=FirstChild Start=1
I'm looking for code which can do the following. Given a snippet of XML,
I have the following XML sample: <finding> <title>Found something</title> <heading>Severity:</heading> <text>Really low.</text> <heading>URL:</heading> <text>https://www.something.com:443</text>

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.