Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 XML in MSSQL

Author  Topic 

muek
Starting Member

16 Posts

Posted - 2009-06-15 : 09:29:39
Hi there,

I have an issue about XML.
I'm trying to extract data from XML, but my attribute can be changed.
I have tried the following but with no sucess.

SELECT pk, xCol.query('/doc[sql:variable("@id") = 123]//section')
FROM docs


Can anyone help me?
Thanks

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-06-15 : 09:38:20
see this example it may helpful

DECLARE @r XML

SET @r = '
<Reference>
<Basic>
<Book>A1.Hj1.JU9</Book>
</Basic>
<App>
<A>AK9.HL9.J0</A>
<A>A18.H.PJ69</A>
</App>
<Sub>
<B>B13.H98.P9</B>
<B>B18.HO9.JIU8</B>
</Sub>
<DI>
<D>D23.HYT.P6R</D>
</DI>
</Reference>
'
--1111111
SELECT node.query('fn:local-name(.)') AS NodeName, node.query('./text()') AS NodeValue
FROM @r.nodes(N'//*') T(node)

SELECT t.n.value('local-name(..)[1]', 'VARCHAR(100)') AS ParentNodeName,
t.n.value('local-name(.)[1]', 'VARCHAR(100)') AS NodeName,
t.n.value('text()[1]', 'VARCHAR(100)') AS NodeText
FROM @r.nodes('/*/*/*') AS t(n)
Go to Top of Page

muek
Starting Member

16 Posts

Posted - 2009-06-15 : 10:18:01
i didn't explain myself correctly.

I have changed your XML, to better showing what I need.

DECLARE @r1 XML
SET @r1 = '
<Reference>
<Basic Book="A1.Hj1.JU9" App="AK9.HL9.J0" />
</Reference>

DECLARE @r2 XML
SET @r2 = '
<Reference>
<Basic App="test2" Name="test1" />
</Reference>

-- I have a function that pass both XMLs and attribute that I want to handle

exec HandleXml @r1, 'Book'
exec HandleXml @r2, 'Name'


I have tried
[code]
-- where @id was 'Book' or 'Name', in this case
SELECT ref.value ('sql:variable("@id")', 'int') as id
FROM @xml.nodes ('/Reference/Basic') R(ref)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-15 : 10:26:17
See http://weblogs.sqlteam.com/peterl/archive/2008/09/01/Updated-XML-search-test-case-with-variables.aspx


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

muek
Starting Member

16 Posts

Posted - 2009-06-16 : 09:52:33
Hi,

is there any way to use xquery/xpath to search a "variable attribute", because all examples that have saw until now, only show avriable attribute value and not "variable attribute".
Go to Top of Page
   

- Advertisement -