Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to extract a column value from data stored in XML column.


How to extract a column value from data stored in XML column.

Author
Message
Vinod Suthar
Vinod Suthar
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 124
I have a XML column having AIF( Application Integration Framework ) Message data. I want to extract invoice number from XML data given below. Please help me.
XML message data is given below:

<Envelope xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/Message">
<Header>
<MessageId>{5603D03A-4380-404D-9F27-738BE0FEA13E}</MessageId>
<Action>http://tempuri.org/LedgerJournalService/create</Action>
</Header>
<Body>
<MessageParts xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/Message">
<LedgerJournal xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/LedgerJournal">
<LedgerJournalTable class="entity">
<CurrencyCode>USD</CurrencyCode>
<JournalName>Day1</JournalName>
<JournalTotalCredit>50</JournalTotalCredit>
<JournalTotalDebit>50</JournalTotalDebit>
<JournalType>Daily</JournalType>
<Name>Daily Journal 1</Name>
<VoucherSeries>Ledger_3</VoucherSeries>
<LedgerJournalTrans class="entity">
<AccountType>Ledger</AccountType>
<AmountCurDebit>50</AmountCurDebit>
<CurrencyCode>USD</CurrencyCode>
<DocumentDate>2007-05-28</DocumentDate>
<Invoice>00003</Invoice>
<OffsetAccount>12020</OffsetAccount>
<OffsetAccountType>Ledger</OffsetAccountType>
<TransDate>2007-05-28</TransDate>
<Txt>AxLedgerJournal inbound test trans 1</Txt>
</LedgerJournalTrans>
</LedgerJournalTable>
</LedgerJournal>
</MessageParts>
</Body>
</Envelope>
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 1777
You can extract specific nodes by use the xml values() method. Additionally, because the data that you have is using a namespace, you also in this instance need to declare those as part of the query and reference them accordingly.


CREATE TABLE #t (XMLDATA XML)
INSERT INTO #t (XMLDATA)
VALUES ('<Envelope xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/Message">
<Header>
<MessageId>{5603D03A-4380-404D-9F27-738BE0FEA13E}</MessageId>
<Action>http://tempuri.org/LedgerJournalService/create</Action>
</Header>
<Body>
<MessageParts xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/Message">
<LedgerJournal xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/LedgerJournal">
<LedgerJournalTable class="entity">
<CurrencyCode>USD</CurrencyCode>
<JournalName>Day1</JournalName>
<JournalTotalCredit>50</JournalTotalCredit>
<JournalTotalDebit>50</JournalTotalDebit>
<JournalType>Daily</JournalType>
<Name>Daily Journal 1</Name>
<VoucherSeries>Ledger_3</VoucherSeries>
<LedgerJournalTrans class="entity">
<AccountType>Ledger</AccountType>
<AmountCurDebit>50</AmountCurDebit>
<CurrencyCode>USD</CurrencyCode>
<DocumentDate>2007-05-28</DocumentDate>
<Invoice>00003</Invoice>
<OffsetAccount>12020</OffsetAccount>
<OffsetAccountType>Ledger</OffsetAccountType>
<TransDate>2007-05-28</TransDate>
<Txt>AxLedgerJournal inbound test trans 1</Txt>
</LedgerJournalTrans>
</LedgerJournalTable>
</LedgerJournal>
</MessageParts>
</Body>
</Envelope>');

WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/dynamics/2008/01/ documents/Message',
   'http://schemas.microsoft.com/dynamics/2008/01/ documents/LedgerJournal' AS LJ)
SELECT XMLDATA.value('(/Envelope/Body/MessageParts/LJ:LedgerJournal/LJ:LedgerJournalTable/LJ:LedgerJournalTrans/LJ:Invoice)[1]', 'varchar(50)') AS 'InvNumber'
FROM #t

DROP TABLE #t



Looking at the data, i'm guessing you may have some repeating sections in there so you may also need to crack open the CROSS APPLY operator as well
Vinod Suthar
Vinod Suthar
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 124
Thank you very much.
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2236 Visits: 7423
arthurolcot (12/23/2013)
You can extract specific nodes by use the xml values() method. Additionally, because the data that you have is using a namespace, you also in this instance need to declare those as part of the query and reference them accordingly.


CREATE TABLE #t (XMLDATA XML)
INSERT INTO #t (XMLDATA)
VALUES ('<Envelope xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/Message">
<Header>
<MessageId>{5603D03A-4380-404D-9F27-738BE0FEA13E}</MessageId>
<Action>http://tempuri.org/LedgerJournalService/create</Action>
</Header>
<Body>
<MessageParts xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/Message">
<LedgerJournal xmlns = "http://schemas.microsoft.com/dynamics/2008/01/
documents/LedgerJournal">
<LedgerJournalTable class="entity">
<CurrencyCode>USD</CurrencyCode>
<JournalName>Day1</JournalName>
<JournalTotalCredit>50</JournalTotalCredit>
<JournalTotalDebit>50</JournalTotalDebit>
<JournalType>Daily</JournalType>
<Name>Daily Journal 1</Name>
<VoucherSeries>Ledger_3</VoucherSeries>
<LedgerJournalTrans class="entity">
<AccountType>Ledger</AccountType>
<AmountCurDebit>50</AmountCurDebit>
<CurrencyCode>USD</CurrencyCode>
<DocumentDate>2007-05-28</DocumentDate>
<Invoice>00003</Invoice>
<OffsetAccount>12020</OffsetAccount>
<OffsetAccountType>Ledger</OffsetAccountType>
<TransDate>2007-05-28</TransDate>
<Txt>AxLedgerJournal inbound test trans 1</Txt>
</LedgerJournalTrans>
</LedgerJournalTable>
</LedgerJournal>
</MessageParts>
</Body>
</Envelope>');

WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/dynamics/2008/01/ documents/Message',
   'http://schemas.microsoft.com/dynamics/2008/01/ documents/LedgerJournal' AS LJ)
SELECT XMLDATA.value('(/Envelope/Body/MessageParts/LJ:LedgerJournal/LJ:LedgerJournalTable/LJ:LedgerJournalTrans/LJ:Invoice)[1]', 'varchar(50)') AS 'InvNumber'
FROM #t

DROP TABLE #t



Looking at the data, i'm guessing you may have some repeating sections in there so you may also need to crack open the CROSS APPLY operator as well


I know this is a little old but you can speed this up dramatically by adding a reference to a text() node at the end of your XPath expression.


WITH XMLNAMESPACES 
(DEFAULT 'http://schemas.microsoft.com/dynamics/2008/01/ documents/Message',
       'http://schemas.microsoft.com/dynamics/2008/01/ documents/LedgerJournal' AS LJ)
SELECT XMLDATA.value('(//LJ:Invoice/text())[1]', 'varchar(50)') AS 'InvNumber'
FROM #t;



-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search