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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:30:20+00:00 2026-05-17T21:30:20+00:00

I’m somewhat new to using XML in sql server, and am having a problem

  • 0

I’m somewhat new to using XML in sql server, and am having a problem getting the syntax right – and for that matter, probably have some basic confusion about referencing XML elements.

Anyway, I’ll need to shred XML like the below out into columns. Seems simple, but I can’t seem to get the nodes right – or for that matter I don’t think I’m doing the xmlnamespaces correctly either. I haven’t found much that’s helpful online yet. Would greatly appreciate any hints!

thanks,
Sylvia

DECLARE @pStepLogXML  xml;
select     @pStepLogXML = '<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2010 (http://www.altova.com)-->
<Applescommon:ActivityStepLogList xsi:schemaLocation="urn:SampleCompany:Apples:common:v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:Applescommon="urn:SampleCompany:Apples:common:v1">
    <Applescommon:ActivityStepLog>
        <Applescommon:GUID>3F2504E0-4F89-11D3-9A0C-0305E82C3301</Applescommon:GUID>
        <Applescommon:ContextID>3F2504E0-4F89-11D3-9A0C-0305E82C3302</Applescommon:ContextID>
        <Applescommon:LogType>PreStep</Applescommon:LogType>
        <Applescommon:SourceID>com.SampleCompany.e3.es.Apples.common.ApplesCommonMessageHandler.processMessage</Applescommon:SourceID>
        <Applescommon:LogContent><![CDATA[
<TestXML><XXXXXX></TestXML>
        ]]></Applescommon:LogContent>
        <Applescommon:LogTime>2001-12-17T09:30:47Z</Applescommon:LogTime>
    </Applescommon:ActivityStepLog>
    <Applescommon:ActivityStepLog>
        <Applescommon:GUID>3F2504E0-4F89-11D3-9A0C-0305E82C3303</Applescommon:GUID>
        <Applescommon:ContextID>3F2504E0-4F89-11D3-9A0C-0305E82C3302</Applescommon:ContextID>
        <Applescommon:LogType>PostStep</Applescommon:LogType>
        <Applescommon:SourceID>com.SampleCompany.e3.es.Apples.common.ApplesCommonMessageHandler.processMessage</Applescommon:SourceID>
        <Applescommon:LogContent><![CDATA[
<TestXML><XXXXXX></TestXML>
        ]]></Applescommon:LogContent>
        <Applescommon:LogTime>2001-12-17T09:30:47Z</Applescommon:LogTime>
    </Applescommon:ActivityStepLog>
</Applescommon:ActivityStepLogList>';

with xmlnamespaces (default 'urn:Applescommon:ActivityStepLogList:v1')
select 
   --convert(varchar(64), MyTables.MyColumns.value('Applescommon:GUID/text()', n'nvarchar(100)')
   -- MyTable.MyColumns.value('Applescommon:GUID/text()', N'nvarchar(50)') AS Test,
   --,
   1
from @pStepLogXML.nodes('/Applescommon:ActivityStepLog' )
    as T(c)

EDIT:

Okay, figured it out. I basically had the xmlnamespaces and nodes completely messed up. I thought I copied them (changing them appropriately) from code that worked, so if anyone would care to point me to a good coherent explanation of these, I’ll give them answer credit.

Also, I wasn’t having problems with getting the column data, so I just put 1 for a placeholder – below I actually put one of the real columns.

Thanks for reading!

Sylvia

with xmlnamespaces (default 'urn:SampleCompany:Apples:common:v1')
select 
   --convert(varchar(64), MyTables.MyColumns.value('Applescommon:GUID/text()', n'nvarchar(100)')
   -- MyTable.MyColumns.value('Applescommon:GUID/text()', N'nvarchar(50)') AS Test,
   --,
   T.c.query('SourceID/text()')
   ,1
from @pStepLogXML.nodes('/ActivityStepLogList/ActivityStepLog' )
    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-17T21:30:20+00:00Added an answer on May 17, 2026 at 9:30 pm

    XQuery isn’t easy, until you get the hang of it – I don’t really have a “magic” site that just tells you everything and then you know…. the best intro article I read was

    SQL Server 2005 XQuery and XML-DML (a 3-part article series)

    and also: An Introduction to XQuery in SQL Server 2005

    Most of all: practice, practice, practice 🙂 Nothing new, right??

    Now your last query is OK – but you could make it even easier:

    with xmlnamespaces (default 'urn:SampleCompany:Apples:common:v1')
    select 
       T.C.value('(GUID)[1]', 'nvarchar(50)') AS Test,
       T.C.value('(ContextID)[1]', 'nvarchar(50)') AS ContextID,
       T.C.value('(SourceID)[1]', 'nvarchar(50)') AS SourceID,
       --,
       1
    from 
        @pStepLogXML.nodes('/ActivityStepLogList/ActivityStepLog') as T(c)
    

    Using the .value() function (instead of .query()) gives you the “atomic” values right away, and you can also define what output data type that value should be.

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

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.