Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


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
XQuery 69
TSQL 67
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
August 2009 19
June 2009 19
May 2010 18
January 2009 15
July 2008 15
January 2010 14
October 2008 14
February 2010 12

XQuery Lab 64 – Reading values from an XML column

Jul 18 2011 2:19AM by Jacob Sebastian   

I got a question in my personal forum this morning requesting help to read values from an XML column. My first reaction was “Well, there is an XQuery lab demonstrating this!”. However, after reviewing the existing XQuery labs, I realized there are no posts demonstrating this.

Here is a simple example that demonstrates how to read values from an XML column.

DECLARE @t TABLE (
	ID INT IDENTITY,
	Data XML
)

INSERT INTO @t (Data)
SELECT '<employee name="Jacob" />' UNION ALL
SELECT '<employee name="Michael" />'

SELECT
	x.value('@Name[1]', 'VARCHAR(20)') AS Name
FROM @t t
CROSS APPLY Data.nodes('/employee') a(x)
/*
Name
--------------------
Jacob
Michael
*/

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

Tags: XML, XQuery, SQL SERVER, BRH, #SQL Server,


Jacob Sebastian
1 · 100% · 22504
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

4  Comments  

  • the code is nice but the result might be nothing since it is a case-sensitive. i think the code should be :

    SELECT x.value('@[1]name[1][1]', 'VARCHAR(20)') AS Name FROM @t t CROSS APPLY Data.nodes('/[1]employee[1]') a(x)

    please correct me if i'm wrong. i'm new in xquery too.

    commented on Feb 22 2012 4:14AM  .  Report Abuse This post is not formatted correctly
    Epon
    2170 · 0% · 3
  • You are right. I just corrected it.

    Thank for catching this.

    commented on Feb 22 2012 4:38AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22504
  • sir i m working on xquery for finding assocation rule using xquery i complete the conding for how i write xquery for that. but now i found a problem that they give me error that "Unexpected token "declare function" beyond end of query" i send me coding part for u plz help me out for how i do.........i m using oxygen xml developer for running the xquery my simple query running proper but we use more than one function in recursive manner they create problem give error Unexpected token "declare function" beyond end of query.

    (: You can activate the content completion by pressing the Ctrl+Space keys. :)
    xquery version "1.0";
    
    (: Namespace for the <oXygen/> custom functions and variables :)
    declare namespace oxy="http://www.oxygenxml.com/xquery/functions";
    declare namespace global="http://www.oxygenxml.com/xquery/functions";
    (: The URI of the document that is to be queried :)
    declare variable $oxy:document-uri as xs:string := "transaction.xml";
    declare variable $oxy:document-to-query as xs:string := "transaction.xml";
    declare variable $local:document-to-query as xs:string := "transaction.xml";
    declare variable $oxy:document as document-node() := doc($oxy:document-uri);
    (:declare function Fp-growth($l,$L,$minsup,$total,$src) as xs:(element,element,minsup,total,src) := "transaction.xml";:)
    
    let $src := doc("transaction.xml")//items
    let $minsup:=2
    let $total:=count($src)*1.00
    let $c:=distinct-values($src/*)
    let $l:=(for $itemset in $c
    let $items:=(for $item in $src/*
    where $itemset=$item
    return $item)
    let $sup:=(count($items)+1)
    where $sup>=$minsup
    return <largeItemset>
    <items> {$items} </items>
    <support> {$sup} </support>
    </largeItemset>)
    let $L:=$l
    return <largeItemsets>{oxyl:FP-growth($l,$L,$minsup,$total,$src)}
    </largeItemsets>
    _/////that part is fine but we we use next fp-growth function they give same error
    
    plz sir help me for solving this////
    
    
    declare function oxy:FP-growth(element $l, element $L, element $minsup, element $total, element $src)
    returns element {
    let $f-item:= first item in $L
    let $l-item:= last item in $L
    let $T:=oxy:getl-itemsets($src, $total, $minsup)
    return <items>
    {oxy:join ($l-item, $T)}
    </items>
    let $l:=$l-items
    let $L:=oxy:remove($l)
    for f-item in $L
    where $f-item != $l-item
    return oxy:FP-growth($l, $L, $minsup, $total, $src)
    }
    
    declare function oxy:getl-itemsets(element $src, element $total, element $minsup)
    returns element {
    let $l:=(for $itemset in $src
    where $itemset=$l-item
    return $itemset)
    let $sup:=(count($l)+1
    where $sup>=$minsup
    return <largeItemset>
    <items> {$item} </items>
    <support> {$sup} </support>
    </largeItemset>
    }
    
    declare function oxy:join(element $X, element $Y) returns element {
    let $items := (for $item in $Y
    where every $i in $X satisfies
    $i != $item
    return $item)
    return $X union $items
    }
    
    declare function oxy:removeDuplicate(element $C) returns element
    {
    for $itemset1 in $C
    let $items1 := $itemset1/*
    let $items :=(for $itemset2 in $C
    let $items2 := $itemset2/*
    where $itemset2>>$itemset1 and
    count($items1) =
    count(commonIts($items1, $items2))
    return $items2)
    where count($items) = 0
    return $itemset1
    }
    
    
    input file for this xquery is:=
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/css" href="personal-schema.css"?>
    <transactions>
    <transaction id="1">
    <items>
    <title>The Green Mile</title>
    <author>Stephen King</author>
    <item>a</item>
    <item>d</item>
    <item>e</item>
    </items>
    </transaction>
    <transaction id="2">
    <items>
    <title>The Green Mile</title>
    <author>J. D. Salinger</author>
    <item>b</item>
    <item>c</item>
    <item>d</item>
    </items>
    </transaction>
    <transaction id="3">
    <items>
    <title>The Green Mile</title>
    <author>Issac Asimov</author>
    <item>a</item>
    <item>c</item>
    <item>e</item>
    </items>
    </transaction>
    <transaction id="4">
    <items>
    <title>The Green Mile</title>
    <author>Isaac Asimov</author>
    <item>b</item>
    <item>c</item>
    <item>d</item>
    </items>
    </transaction>
    <transaction id="5">
    <items>
    <title>The Green Mile</title>
    <author>Oscar Wilde</author>
    <item>a</item>
    <item>b</item>
    
    </items>
    </transaction>
    </transactions>
    
    commented on Mar 13 2012 4:23AM  .  Report Abuse This post is not formatted correctly
    malikvnit
    2307 · 0% · 2
  • Unfortunately, I am not familiar with the environment you mentioned. My interaction with XML is mostly in and around SQL server.

    commented on Mar 13 2012 4:41AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22504

Your Comment


Sign Up or Login to post a comment.

"XQuery Lab 64 – Reading values from an XML column" rated 5 out of 5 by 3 readers
XQuery Lab 64 – Reading values from an XML column , 5.0 out of 5 based on 3 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising