Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

XQuery Lab 55 – Extracting values from XML elements that matches a string pattern

Apr 7 2010 9:48AM by Jacob Sebastian   

Here is another XQuery exercise that uses the contains() function to match a string pattern against an XML element and retrieves values from a sibling element.

Here is the input data for this challenge.

<ArrayOfCustomField>
      <CustomField>
            <Name>abc</Name>
            <Value>Some Value</Value>
      </CustomField>
      <CustomField>
            <Name>SKU,AlphaNumeric,20</Name>
            <Value>XXX111</Value>
      </CustomField>
      <CustomField>
            <Name>Another Name</Name>
            <Value>Another Value</Value>
      </CustomField>
</ArrayOfCustomField>

The task is to retrieve the text from the Value element where the Name contains “SKU”. In this case, the result needs to be “XXX111”.

Here is the query that performs the above task.

DECLARE @x XML
SELECT @x = '
<ArrayOfCustomField>
      <CustomField>
            <Name>abc</Name>
            <Value>Some Value</Value>
      </CustomField>
      <CustomField>
            <Name>SKU,AlphaNumeric,20</Name>
            <Value>XXX111</Value>
      </CustomField>
      <CustomField>
            <Name>Another Name</Name>
            <Value>Another Value</Value>
      </CustomField>
</ArrayOfCustomField>'

SELECT
	x.value('Value[1]','VARCHAR(30)') AS value
FROM @x.nodes('/ArrayOfCustomField/CustomField') a(x)
CROSS APPLY x.nodes('Name[contains(.,"SKU")]') b(y)

/*
value
------------------------------
XXX111
*/

View All Labs: XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials

Visit the XML Resource Center for more XML articles and tutorials.

Tags: XQuery-Functions, XQuery, XQuery Functions, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial, XQuery-Labsxml,


Jacob Sebastian
1 · 100% · 32004
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

5  Comments  

  • Hi Jacob, In this example if i wanted to look for an integer value, how would i achieve it. Instead of Name say i had a element called ID and i wanted the value for ID = 20. could you let me know.

    Thank you Ramdas

    commented on Apr 16 2010 3:55PM
    Ramdas
    703 · 0% · 46
  • Ramdas, You can achieve it by doing a comparison using "=" operator. If you can send me a sample XML, I can show you how to do that.

    commented on Apr 17 2010 4:22AM
    Jacob Sebastian
    1 · 100% · 32004
  • Or post your example here: http://beyondrelational.com/forums/80.aspx so that this can be helpful for more people.

    commented on Apr 17 2010 4:23AM
    Jacob Sebastian
    1 · 100% · 32004
  • DECLARE @x XML SELECT @x = ' 100 abc Some Value SKU,AlphaNumeric,20 XXX111 Another Name Another Value '

    SELECT x.value('Value[1]','VARCHAR(30)') AS value FROM @x.nodes('/ArrayOfCustomField/CustomField') a(x) CROSS APPLY x.nodes('id[.=100]') b(y)

    commented on Jun 17 2010 9:26AM
    sanjay
    190 · 1% · 246
  • So you found the solution..right?

    commented on Oct 16 2010 3:40PM
    JacobSebastian
    47 · 4% · 1215

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]