How to read an atribute from xml field

  • I did some quick search but no luck so far, here is my data in xml format, I need a function fnGetAttribut(@xml, @field) that can return a specific field in the xml string, original xml sample data is as follow:

    Thanks very much in advance.

    keyword1.xxx

    value1

    keyword2.yyy

    value2

    keyword3.aaa

    value3

    keyword4.bbb

    value4

    ....

  • A few questions:

    a) What have you tried so far and where did you get stuck?

    b) What should be the data type returned by the function?

    c) What would you expect if someone would query for "hashmap" or a nonexisting node?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks for replying.

    I didn't know the original data was modified automatically by the system, with the IFCode markup, the data should be:

    keyword1.xxx

    value1

    keyword2.yyy

    value2

    keyword3.aaa

    value3

    keyword4.bbb

    value4

    ....

    There are some other solutions but they are all like:

    DECLARE @data XML;

    SET @data =

    '

    14

    2260586.523

    0.018

    ';

    SELECT @data.value('(/*/Result/EngineInfo/ModelDataVersion)[1]', 'NVARCHAR(10)') ModelDataVersion;

    So you can see my data is a little special because they all have the same keyword

    Using my sample data as example, ideally I need the function works like:

    fnGetDate(@data, @field)

    fnGetDate(@data, 'keyword1.xxx') should return 'value1'

  • Please note that nobody modified your data. The display being different from what you posted is caused by the forum software and because you may have not used the XML tags around your xml data Please use [ code="xml" ][ /code ] without blanks.

    Following please find the question I asked before that are not answered yet:

    lmu92 (9/23/2009)


    A few questions:

    a) What have you tried so far and where did you get stuck?

    b) What should be the data type returned by the function?

    c) What would you expect if someone would query for "hashmap" or a nonexisting node?



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • I already answered your question. Thanks.

  • For question c: this function is only used by me, so I wouldnot query a non-existing node.

  • halifaxdal (9/23/2009)


    For question c: this function is only used by me, so I wouldnot query a non-existing node.

    That doesn't answer what the function should return if you'd enter a wrong value (other than you're the first person I ever talked to who never had and never will call a function with a typo in it, or, even more likely since XML is case sensitive, mixed upper and lower case.)

    But let's assume you'd be able to guarantee it:

    I still can't find an answer to question a).

    Side note:

    Normally, xml data are not supposed to be ordered in any way. If elements belong together, they should be nested.

    AFAIK he xml concept would consider the two xml documents

    keyword1.xxx

    value1

    keyword2.yyy

    value2

    keyword3.aaa

    value3

    keyword4.bbb

    value4

    ....

    and

    keyword1.xxx

    keyword2.yyy

    keyword3.aaa

    keyword4.bbb

    value4

    value1

    value2

    value3

    ....

    as being identical, since all nodes are at the same node level.

    So you might want to try to get well formed xml data in the first place. This would make it a lot easier to query against.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks for your reply. Question a was answered already, example was given in the post, and here again for your convenience:

    DECLARE @data XML;

    SET @data =

    '

    14

    2260586.523

    0.018

    ';

    SELECT @data.value('(/*/Result/EngineInfo/ModelDataVersion)[1]', 'NVARCHAR(10)') ModelDataVersion;

  • As stated in my previous post: xml is case sensitive.

    If you change your query syntax to EXACTLY match the node names, it'll return 14.

    Edit: Hint: CamelCase vs. lowercase.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • lmu92 (9/23/2009)


    As stated in my previous post: xml is case sensitive.

    If you change your query syntax to EXACTLY match the node names, it'll return 14.

    Edit: Hint: CamelCase vs. lowercase.

    Don't know what you are talking about, I am working on my own way now, thank you anyway.

  • halifaxdal (9/23/2009)


    Don't know what you are talking about.

    We're obviously having some misunderstanding because I cannot figure out what your real question is:

    In your first post you provided a non-well-formed xml document.

    Later on you posted a well formed document with totally different content.

    It's still unclear, what type of document you want to query against - first or second one.

    Please note that we can't look over your shoulder to see your data so you have to help us help you.

    Maybe you should search for "XML Workshop" on this site.

    It will guide you to a list of articles by Jacob Sebastian on how to work with xml data.

    If you need further assistance please read and follow the description in the link in my signature on how to post data to get fast assistance.

    Summarizing: If we don't know what you're looking for, we can't help you.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply