Showing posts with label xquery. Show all posts
Showing posts with label xquery. Show all posts

Friday, March 9, 2012

Problem Xquery about XML node called 'data'

I have a xml code:
<root>
<data id="1">
<value>80</value>
</data>
</root>

I execute the following query XQuery in C# .NET:

For $b IN document(\"filename.xml\")/root/data
WHERE $b/@.id =\"1\" RETURN $b/value

and XQuery show me the following error:

'(' expected. Maybe you are using a known function name like last, data, text etc as an identifier. For this release , this is not allowed.

I can't change the name "data" for my Xml Node so
how can I solve my problem using the word "data" in XQuery?

Dear Luigi

Note that the XQuery engine in C# .Net was just a technology preview and is not supported.

One way to work around your problem is to replace /root/data with /root/*[local-name() = "data"].

Best regards
Michael|||Thank you,
but function local-name() generate an error:

User Defined Functions not supported

This error is try with other function eg. concat, tokenize, upper-case, etc.

I using XQuery Demo: Microsoft.Xml.XQuery.dll (19/02/2002)

Exists one more recent version?

|||You may need to use fn:local-name() instead.

Note that the demo is not supported and no newer version is available.

Wednesday, March 7, 2012

Problem with XQuery...

Hi all,

I've a little problem to read from a XML string using T-Sql, someone can help me please?
I use this XML string:
DECLARE @.x xml
SET @.x = '
<Envelope xmlns="urn:aerospace: dataschema: DocumentBusinessInformationEntitySchemaModule" xmlns:asram="urn: aerospaceBig Smileataschema: ReusableAggregateBusinessInformationEntitySchemaModule">
<ROOT>
<asram:a>111</asram:a>
</ROOT>
</Envelope>
'

And this select :
SELECT @.x.query('/Envelope/ROOT/asram:a/text()') AS LastResult

Whow I can get de “111”?

Tks

In your query you need to make sure to declare the namespaces:

Code Snippet

DECLARE @.x xml;

SET @.x = '

<Envelope xmlns="urn:aerospace: dataschema: DocumentBusinessInformationEntitySchemaModule" xmlns:asram="urn: aerospace dataschema: ReusableAggregateBusinessInformationEntitySchemaModule">

<ROOT>

<asram:a>111</asram:a>

</ROOT>

</Envelope>;

'

SELECT @.x.query('

declare default element namespace "urn:aerospace: dataschema: DocumentBusinessInformationEntitySchemaModule";

declare namespace asram="urn: aerospace dataschema: ReusableAggregateBusinessInformationEntitySchemaModule";

/Envelope/ROOT/asram:a/text()

') AS LastResult;

|||Thank, that works,

But if i change the <Envelope> like that (
<Envelope xmlns="urn:aerospace: dataschema: documentBusinessInformationEntitySchemaModule" xmlns:asram="urn:aerospace: dataschema:ReusableAggregateBusinessInformationEntitySchemaModule" xmlns:ccts="urn:unece:uncefact: dataschema: draft:CoreComponentTypesSchemaModule:0:3:4" xmlns:udt="urn:unece:uncefact: dataschema: draft:UnqualifiedDataTypesSchemaModule:0:3:4" xmlns:mime="urn:unece:uncefact: dataschema: draft:codeList:MIME:MIME_Media_Types:2003::IANA" xmlns:rec20="urn:unece:uncefact: dataschema: draft:codeList:Rec20:Codes_for_Units_of_Measure:2001:6:UNECE" xmlns:iso4217="urn:unece:uncefact: dataschema: draft:identifierData:ISO_4217:Currency_Codes:2001:5:ISO" xmlns:iso639="urn:unece:uncefact: dataschema: draft:identifierData:ISO_639:Language_Code:1988:3:ISO" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsiTongue TiedchemaLocation="urn:aerospace: dataschema: documentBusinessInformationEntitySchemaModule
\\Srci-gestion\SRCI\SRCI_Projets\Associations\CAPEMP~1\Norme_CapEmploi\R1.13\CapEmploi_Envelope_BIE_R1.13.xsd">
)
how I can get again the “111”|||

The only change seems to be in the namespac URNs so you need to adapt the XQuery namespace declarations as in this example:

Code Snippet

DECLARE @.x xml;

SET @.x = '

<Envelope xmlns="urn:aerospace: dataschema: documentBusinessInformationEntitySchemaModule" xmlns:asram="urn:aerospace: dataschema:ReusableAggregateBusinessInformationEntitySchemaModule" xmlns:ccts="urn:unece:uncefact: dataschema: draft:CoreComponentTypesSchemaModule:0:3:4" xmlns:udt="urn:unece:uncefact: dataschema: draft:UnqualifiedDataTypesSchemaModule:0:3:4" xmlns:mime="urn:unece:uncefact: dataschema: draft:codeList:MIME:MIME_Media_Types:2003::IANA" xmlns:rec20="urn:unece:uncefact: dataschema: draft:codeList:Rec20:Codes_for_Units_of_Measure:2001:6:UNECE" xmlns:iso4217="urn:unece:uncefact: dataschema: draft:identifierData:ISO_4217:Currency_Codes:2001:5:ISO" xmlns:iso639="urn:unece:uncefact: dataschema: draft:identifierData:ISO_639:Language_Code:1988:3:ISO" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:aerospace: dataschema: documentBusinessInformationEntitySchemaModule\\Srci-gestion\SRCI\SRCI_Projets\Associations\CAPEMP~1\Norme_CapEmploi\R1.13\CapEmploi_Envelope_BIE_R1.13.xsd">

<ROOT>

<asram:a>111</asram:a>

</ROOT>

</Envelope>;

'

SELECT @.x.query('

declare default element namespace "urn:aerospace: dataschema: documentBusinessInformationEntitySchemaModule";

declare namespace asram="urn:aerospace: dataschema:ReusableAggregateBusinessInformationEntitySchemaModule";

/Envelope/ROOT/asram:a/text()

') AS LastResult;

|||It works, thanks a lot.
(Merci beaucoup)

Problem with XQuery Syntax

I'm storing addresses in an XML address column, and I need to be able
to do partial searches...
For example...
declare @.tAddress table (Address xml DEFAULT '<Address Record />' )
INSERT INTO @.tAddress (Address)
VALUES ('<AddressType name="Shipping">
<AddressRecord StreetLine1="111 Main"
City="Houston"
State="TX"
ZipCode = "11111" />
</AddressType> ');
A single address could have multiple address types (billing and
shipping) and multiple entries for each one.
I know how to query for an exact match on a column...
SELECT Address FROM @.tAddress WHERE
Address.exist('/AddressType/AddressRecord[ @.City = "Houston" ]') =1
However...I'm getting errors when I try to use a contains (in the
same way I'd use a City Like '%ous%'
How would I code a contains on any particular column?
Thanks!
KevinFollow-up...
I tried the following:
SELECT * FROM @.tAddress
where
address.exist('/AddressType/AddressRecord/City/text()[contains(.,"Houston")]
')
= 1
It doesn't bomb, but it generates zero results.
I'm sure I'm missing something, but don't know what.
Kevin|||Try this
SELECT * FROM @.tAddress
where
address.exist('/AddressType/AddressRecord [contains(@.City,"Houston")]')
= 1|||On 10 Mar 2006 12:08:09 -0800, markc600@.hotmail.com wrote:

>Try this
>SELECT * FROM @.tAddress
>where
>address.exist('/AddressType/AddressRecord [contains(@.City,"Houston")]')
>= 1
THAT WORKED!!!!!
You're the man!!!!!!!!!!!
THANK YOU!!!!!!!
Kevin