XPath expression to pluck out attribute value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have the following XML:



<envelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="vendor">
<fizz buzz="3"/>
</payload>
</envelope>


I am trying to write an XPath expression that will pluck out vendor (value for the payload's class attribute) or whatever its value is.



My best attempts are:



/dataEnvelope/payload[@class="vendor"]@class


But this requires the expression to already know that vendor is the value of the attribute. But if the XML is:



<dataEnvelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="foobar">
<fizz buzz="3"/>
</payload>
</dataEnvelope>


Then I want the expression to pluck out the foobar. Any ideas where I'm going awry?










share|improve this question


















  • 1





    Which data you know initially? What you can use to identify required element? action value, auditId,... ?

    – Andersson
    Nov 16 '18 at 19:31













  • Is the difference between the two XML examples intentional? If so, state that you need to be able to handle that difference when selecting the @vendor value. You can accommodate the difference in the XPath, if necessary. Otherwise, it would be less confusing for those answering if you didn't have <envelope> and <dataEnvelope> different.

    – Mads Hansen
    Nov 17 '18 at 2:57


















0















I have the following XML:



<envelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="vendor">
<fizz buzz="3"/>
</payload>
</envelope>


I am trying to write an XPath expression that will pluck out vendor (value for the payload's class attribute) or whatever its value is.



My best attempts are:



/dataEnvelope/payload[@class="vendor"]@class


But this requires the expression to already know that vendor is the value of the attribute. But if the XML is:



<dataEnvelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="foobar">
<fizz buzz="3"/>
</payload>
</dataEnvelope>


Then I want the expression to pluck out the foobar. Any ideas where I'm going awry?










share|improve this question


















  • 1





    Which data you know initially? What you can use to identify required element? action value, auditId,... ?

    – Andersson
    Nov 16 '18 at 19:31













  • Is the difference between the two XML examples intentional? If so, state that you need to be able to handle that difference when selecting the @vendor value. You can accommodate the difference in the XPath, if necessary. Otherwise, it would be less confusing for those answering if you didn't have <envelope> and <dataEnvelope> different.

    – Mads Hansen
    Nov 17 '18 at 2:57














0












0








0








I have the following XML:



<envelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="vendor">
<fizz buzz="3"/>
</payload>
</envelope>


I am trying to write an XPath expression that will pluck out vendor (value for the payload's class attribute) or whatever its value is.



My best attempts are:



/dataEnvelope/payload[@class="vendor"]@class


But this requires the expression to already know that vendor is the value of the attribute. But if the XML is:



<dataEnvelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="foobar">
<fizz buzz="3"/>
</payload>
</dataEnvelope>


Then I want the expression to pluck out the foobar. Any ideas where I'm going awry?










share|improve this question














I have the following XML:



<envelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="vendor">
<fizz buzz="3"/>
</payload>
</envelope>


I am trying to write an XPath expression that will pluck out vendor (value for the payload's class attribute) or whatever its value is.



My best attempts are:



/dataEnvelope/payload[@class="vendor"]@class


But this requires the expression to already know that vendor is the value of the attribute. But if the XML is:



<dataEnvelope>
<action>INSERT</action>
<auditId>123</auditId>
<payload class="foobar">
<fizz buzz="3"/>
</payload>
</dataEnvelope>


Then I want the expression to pluck out the foobar. Any ideas where I'm going awry?







xpath






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 19:25









hotmeatballsouphotmeatballsoup

204314




204314








  • 1





    Which data you know initially? What you can use to identify required element? action value, auditId,... ?

    – Andersson
    Nov 16 '18 at 19:31













  • Is the difference between the two XML examples intentional? If so, state that you need to be able to handle that difference when selecting the @vendor value. You can accommodate the difference in the XPath, if necessary. Otherwise, it would be less confusing for those answering if you didn't have <envelope> and <dataEnvelope> different.

    – Mads Hansen
    Nov 17 '18 at 2:57














  • 1





    Which data you know initially? What you can use to identify required element? action value, auditId,... ?

    – Andersson
    Nov 16 '18 at 19:31













  • Is the difference between the two XML examples intentional? If so, state that you need to be able to handle that difference when selecting the @vendor value. You can accommodate the difference in the XPath, if necessary. Otherwise, it would be less confusing for those answering if you didn't have <envelope> and <dataEnvelope> different.

    – Mads Hansen
    Nov 17 '18 at 2:57








1




1





Which data you know initially? What you can use to identify required element? action value, auditId,... ?

– Andersson
Nov 16 '18 at 19:31







Which data you know initially? What you can use to identify required element? action value, auditId,... ?

– Andersson
Nov 16 '18 at 19:31















Is the difference between the two XML examples intentional? If so, state that you need to be able to handle that difference when selecting the @vendor value. You can accommodate the difference in the XPath, if necessary. Otherwise, it would be less confusing for those answering if you didn't have <envelope> and <dataEnvelope> different.

– Mads Hansen
Nov 17 '18 at 2:57





Is the difference between the two XML examples intentional? If so, state that you need to be able to handle that difference when selecting the @vendor value. You can accommodate the difference in the XPath, if necessary. Otherwise, it would be less confusing for those answering if you didn't have <envelope> and <dataEnvelope> different.

– Mads Hansen
Nov 17 '18 at 2:57












3 Answers
3






active

oldest

votes


















1














If you need @class value from payload node, you can use



/dataEnvelope/payload[@class]/@class


or just



/dataEnvelope/payload/@class





share|improve this answer































    0














    At first, your two XML files are out-of-sync - one references envelope and the other references dataEnvelope. So exchange one for the other, if necessary.



    So, to get the attribute value of payload, you can use an XPath expression like this which uses a child's attribute value to be more specific:



    /envelope/payload[fizz[@buzz='3']]/@class


    Output is:



    vendor





    share|improve this answer































      0














      If the document element can/will change, then you can keep the XPath more generic and select the value of the class attribute from the payload element that is a child of any element:



      /*/payload/@class


      If you know that it will always be a child of envelope, then this would be more specific(but the above would still work):



      /envelope/payload/@class





      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344182%2fxpath-expression-to-pluck-out-attribute-value%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        If you need @class value from payload node, you can use



        /dataEnvelope/payload[@class]/@class


        or just



        /dataEnvelope/payload/@class





        share|improve this answer




























          1














          If you need @class value from payload node, you can use



          /dataEnvelope/payload[@class]/@class


          or just



          /dataEnvelope/payload/@class





          share|improve this answer


























            1












            1








            1







            If you need @class value from payload node, you can use



            /dataEnvelope/payload[@class]/@class


            or just



            /dataEnvelope/payload/@class





            share|improve this answer













            If you need @class value from payload node, you can use



            /dataEnvelope/payload[@class]/@class


            or just



            /dataEnvelope/payload/@class






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 19:29









            AnderssonAndersson

            39.2k113669




            39.2k113669

























                0














                At first, your two XML files are out-of-sync - one references envelope and the other references dataEnvelope. So exchange one for the other, if necessary.



                So, to get the attribute value of payload, you can use an XPath expression like this which uses a child's attribute value to be more specific:



                /envelope/payload[fizz[@buzz='3']]/@class


                Output is:



                vendor





                share|improve this answer




























                  0














                  At first, your two XML files are out-of-sync - one references envelope and the other references dataEnvelope. So exchange one for the other, if necessary.



                  So, to get the attribute value of payload, you can use an XPath expression like this which uses a child's attribute value to be more specific:



                  /envelope/payload[fizz[@buzz='3']]/@class


                  Output is:



                  vendor





                  share|improve this answer


























                    0












                    0








                    0







                    At first, your two XML files are out-of-sync - one references envelope and the other references dataEnvelope. So exchange one for the other, if necessary.



                    So, to get the attribute value of payload, you can use an XPath expression like this which uses a child's attribute value to be more specific:



                    /envelope/payload[fizz[@buzz='3']]/@class


                    Output is:



                    vendor





                    share|improve this answer













                    At first, your two XML files are out-of-sync - one references envelope and the other references dataEnvelope. So exchange one for the other, if necessary.



                    So, to get the attribute value of payload, you can use an XPath expression like this which uses a child's attribute value to be more specific:



                    /envelope/payload[fizz[@buzz='3']]/@class


                    Output is:



                    vendor






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 19:30









                    zx485zx485

                    15.4k133248




                    15.4k133248























                        0














                        If the document element can/will change, then you can keep the XPath more generic and select the value of the class attribute from the payload element that is a child of any element:



                        /*/payload/@class


                        If you know that it will always be a child of envelope, then this would be more specific(but the above would still work):



                        /envelope/payload/@class





                        share|improve this answer




























                          0














                          If the document element can/will change, then you can keep the XPath more generic and select the value of the class attribute from the payload element that is a child of any element:



                          /*/payload/@class


                          If you know that it will always be a child of envelope, then this would be more specific(but the above would still work):



                          /envelope/payload/@class





                          share|improve this answer


























                            0












                            0








                            0







                            If the document element can/will change, then you can keep the XPath more generic and select the value of the class attribute from the payload element that is a child of any element:



                            /*/payload/@class


                            If you know that it will always be a child of envelope, then this would be more specific(but the above would still work):



                            /envelope/payload/@class





                            share|improve this answer













                            If the document element can/will change, then you can keep the XPath more generic and select the value of the class attribute from the payload element that is a child of any element:



                            /*/payload/@class


                            If you know that it will always be a child of envelope, then this would be more specific(but the above would still work):



                            /envelope/payload/@class






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 17 '18 at 3:00









                            Mads HansenMads Hansen

                            45.1k1195123




                            45.1k1195123






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344182%2fxpath-expression-to-pluck-out-attribute-value%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Lugert, Oklahoma