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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:02:48+00:00 2026-05-28T13:02:48+00:00

I would like to read the xsi:type attribute from the current node in a

  • 0

I would like to read the xsi:type attribute from the “current” node in a SELECT statement. My XML looks like this:

 <ns2:data xmlns:ns2="http://mynamespace">
  <OrderLineItems>
    <OrderLineItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:DeactivationLineItem" id="1">
      <Product>
        <Id>5300</Id>
        <Description>DUMMY</Description>
        <Domain>ddd</Domain>
      </Product>
      <Quantity>1</Quantity>
    </OrderLineItem>
    <OrderLineItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:ActivationLineItem" id="4">
      <Product>
        <Id>5340</Id>
        <Description>DUMMY</Description>
        <Domain>aaa</Domain>
      </Product>
      <Quantity>1</Quantity>
    </OrderLineItem>
    <OrderLineItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:DeactivationLineItem" id="12">
      <Product>
        <Id>53200</Id>
        <Description>DUMMY</Description>
        <Domain>ccc</Domain>
      </Product>
      <Quantity>21</Quantity>
    </OrderLineItem>
  </OrderLineItems>
</ns2:data>

My select statement looks as follows:

;WITH XMLNAMESPACES('http://mynamespace' AS ns)
SELECT
,OrderLineItemID                = ref.value('@id', 'int')
,OrderLineItemParentID          = ref.value('@parentId', 'int')
,ProductID                      = NULLIF(ref.query('Product/Id').value('.', 'varchar(255)'),'')
,ProductDescription             = NULLIF(ref.query('Product/Description').value('.', 'varchar(255)'),'')
,ProductDomain                  = NULLIF(ref.query('Product/Domain').value('.', 'varchar(255)'),'')
,ProductAdditionalInfo          = NULLIF(ref.query('Product/AdditionalInfo').value('.', 'varchar(255)'),'')
,Quantity                       = ref.query('Quantity').value('.', 'int')

,LineItemType                   = ref.value('@xsi:type','varchar(max)')                 

FROM tTEMP_XMLTABLE
CROSS APPLY xmlFile.nodes('/ns:data/OrderLineItems/OrderLineItem') R(ref) 

My problem is the line LineItemType as it throws an error:
The XQuery syntax ‘@{http://www.w3.org/2001/XMLSchema-instance}:type’ is not supported

It is strange, because I am able to read a single type if I don’t use the CROSS APPLY:

WITH  XMLNAMESPACES ('http://mynamespace' as p)
SELECT CAST(xmlFile as XML).value('(/p:data/OrderLineItems/OrderLineItem/@xsi:type)[1]','nvarchar(max)')
from tTEMP_XMLTABLE;

The second statement works on SQL Server 2005. Is there a possibility to read the xsi:type attribute when using cross apply?

Thanks for the help

  • 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-28T13:02:48+00:00Added an answer on May 28, 2026 at 1:02 pm

    This worked for me in SQL Server 2008 SP1 (you didn’t specify version, so I’m not sure if that is what you have or not). Not sure if it’s possible for you to get your xml or a copy of your xml into an untyped xml field like I have here, but that might help you get around the schema-bound issue from the link I gave you above.

    DECLARE @tmp_xml TABLE (id int identity, data xml)
    
    INSERT INTO @tmp_xml (data)
    VALUES ('<ns2:data xmlns:ns2="http://mynamespace">
      <OrderLineItems>
        <OrderLineItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:DeactivationLineItem" id="1">
          <Product>
            <Id>5300</Id>
            <Description>DUMMY</Description>
            <Domain>ddd</Domain>
          </Product>
          <Quantity>1</Quantity>
        </OrderLineItem>
        <OrderLineItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:ActivationLineItem" id="4">
          <Product>
            <Id>5340</Id>
            <Description>DUMMY</Description>
            <Domain>aaa</Domain>
          </Product>
          <Quantity>1</Quantity>
        </OrderLineItem>
        <OrderLineItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:DeactivationLineItem" id="12">
          <Product>
            <Id>53200</Id>
            <Description>DUMMY</Description>
            <Domain>ccc</Domain>
          </Product>
          <Quantity>21</Quantity>
        </OrderLineItem>
      </OrderLineItems>
    </ns2:data>')
    
    ;WITH XMLNAMESPACES('http://mynamespace' AS ns)
    SELECT
    OrderLineItemID                = ref.value('@id', 'int')
    ,OrderLineItemParentID          = ref.value('@parentId', 'int')
    ,ProductID                      = NULLIF(ref.query('Product/Id').value('.', 'varchar(255)'),'')
    ,ProductDescription             = NULLIF(ref.query('Product/Description').value('.', 'varchar(255)'),'')
    ,ProductDomain                  = NULLIF(ref.query('Product/Domain').value('.', 'varchar(255)'),'')
    ,ProductAdditionalInfo          = NULLIF(ref.query('Product/AdditionalInfo').value('.', 'varchar(255)'),'')
    ,Quantity                       = ref.query('Quantity').value('.', 'int')
    
    ,LineItemType                   = ref.value('@xsi:type','varchar(max)')                 
    
    FROM @tmp_xml t
        CROSS APPLY data.nodes('/ns:data/OrderLineItems/OrderLineItem') R(ref) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read/write encrypted XML files using LINQ to XML. Does anyone
I would like to read strings into Matlab from an excel file ID =
I would like to read some data from a stream I have using std::getline
I would like to read an xml file. I' ve found an example which
I would like to read the value from the e.Item.DataItem into a string but
I would like to read a string from a TCP stream that is given
I would like to read a EAN-13 bar-code from an image in C. I
I would like to read each line of text from the file outputted from
I would like to read data from a plist, add some elements and the
I would like to read id3 tags from mp3 files in a folder with

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.