XPath Get Value based on sibling value - with namespace



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








2















There are lots of answers with no namespaces involved but I'm having trouble with the namespace.



Here's the xml doc:



<GetServiceRequestDetailsResponse xmlns="http://lcc.catscrm">
<GetServiceRequestDetailsResult xmlns:a="http://schemas.datacontract.org/2004/07/LCC.CATS.CRM.WCF.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:AltRefs>
<a:AltRefModel>
<a:Name>UTRN</a:Name>
<a:TypeId>7577</a:TypeId>
<a:Value>9024930</a:Value>
</a:AltRefModel>
</a:AltRefs>
<a:Customer>
<a:Address>
<a:Address1>5</a:Address1>
<a:Address2>Random</a:Address2>
<a:County>West Yorks</a:County>
<a:NumberName>5</a:NumberName>
<a:Postcode>L1</a:Postcode>
<a:TownCity>London</a:TownCity>
<a:UPRN i:nil="true"/>
</a:Address>
<a:AltRefs/>
<a:ContactTransports>
<a:ContactTransport>
<a:Name>MobileNumber</a:Name>
<a:TypeId>3</a:TypeId>
<a:Value>08965</a:Value>
</a:ContactTransport>
<a:ContactTransport>
<a:Name>Email</a:Name>
<a:TypeId>1</a:TypeId>
<a:Value>a@b.com</a:Value>
</a:ContactTransport>
</a:ContactTransports>
<a:CustomerUPRN i:nil="true"/>
<a:FirstName>Rob</a:FirstName>
<a:Id i:nil="true"/>
<a:LastName>Bowman</a:LastName>
</a:Customer>
</GetServiceRequestDetailsResult>




I need to get the /ContractTransport/Value where the /ContractTransport/Name='Email'.



The following xpath gets me to ContactTransport /*[local-name()='GetServiceRequestDetailsResponse']/*[local-name()='GetServiceRequestDetailsResult']/*[local-name()='Customer']/*[local-name()='ContactTransports']/*[local-name()='ContactTransport']



If there were no namespaces then I guess I could suffix with [Name='Email]/*[local-name()='Value'] but this doesn't work now.










share|improve this question




























    2















    There are lots of answers with no namespaces involved but I'm having trouble with the namespace.



    Here's the xml doc:



    <GetServiceRequestDetailsResponse xmlns="http://lcc.catscrm">
    <GetServiceRequestDetailsResult xmlns:a="http://schemas.datacontract.org/2004/07/LCC.CATS.CRM.WCF.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:AltRefs>
    <a:AltRefModel>
    <a:Name>UTRN</a:Name>
    <a:TypeId>7577</a:TypeId>
    <a:Value>9024930</a:Value>
    </a:AltRefModel>
    </a:AltRefs>
    <a:Customer>
    <a:Address>
    <a:Address1>5</a:Address1>
    <a:Address2>Random</a:Address2>
    <a:County>West Yorks</a:County>
    <a:NumberName>5</a:NumberName>
    <a:Postcode>L1</a:Postcode>
    <a:TownCity>London</a:TownCity>
    <a:UPRN i:nil="true"/>
    </a:Address>
    <a:AltRefs/>
    <a:ContactTransports>
    <a:ContactTransport>
    <a:Name>MobileNumber</a:Name>
    <a:TypeId>3</a:TypeId>
    <a:Value>08965</a:Value>
    </a:ContactTransport>
    <a:ContactTransport>
    <a:Name>Email</a:Name>
    <a:TypeId>1</a:TypeId>
    <a:Value>a@b.com</a:Value>
    </a:ContactTransport>
    </a:ContactTransports>
    <a:CustomerUPRN i:nil="true"/>
    <a:FirstName>Rob</a:FirstName>
    <a:Id i:nil="true"/>
    <a:LastName>Bowman</a:LastName>
    </a:Customer>
    </GetServiceRequestDetailsResult>




    I need to get the /ContractTransport/Value where the /ContractTransport/Name='Email'.



    The following xpath gets me to ContactTransport /*[local-name()='GetServiceRequestDetailsResponse']/*[local-name()='GetServiceRequestDetailsResult']/*[local-name()='Customer']/*[local-name()='ContactTransports']/*[local-name()='ContactTransport']



    If there were no namespaces then I guess I could suffix with [Name='Email]/*[local-name()='Value'] but this doesn't work now.










    share|improve this question
























      2












      2








      2








      There are lots of answers with no namespaces involved but I'm having trouble with the namespace.



      Here's the xml doc:



      <GetServiceRequestDetailsResponse xmlns="http://lcc.catscrm">
      <GetServiceRequestDetailsResult xmlns:a="http://schemas.datacontract.org/2004/07/LCC.CATS.CRM.WCF.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <a:AltRefs>
      <a:AltRefModel>
      <a:Name>UTRN</a:Name>
      <a:TypeId>7577</a:TypeId>
      <a:Value>9024930</a:Value>
      </a:AltRefModel>
      </a:AltRefs>
      <a:Customer>
      <a:Address>
      <a:Address1>5</a:Address1>
      <a:Address2>Random</a:Address2>
      <a:County>West Yorks</a:County>
      <a:NumberName>5</a:NumberName>
      <a:Postcode>L1</a:Postcode>
      <a:TownCity>London</a:TownCity>
      <a:UPRN i:nil="true"/>
      </a:Address>
      <a:AltRefs/>
      <a:ContactTransports>
      <a:ContactTransport>
      <a:Name>MobileNumber</a:Name>
      <a:TypeId>3</a:TypeId>
      <a:Value>08965</a:Value>
      </a:ContactTransport>
      <a:ContactTransport>
      <a:Name>Email</a:Name>
      <a:TypeId>1</a:TypeId>
      <a:Value>a@b.com</a:Value>
      </a:ContactTransport>
      </a:ContactTransports>
      <a:CustomerUPRN i:nil="true"/>
      <a:FirstName>Rob</a:FirstName>
      <a:Id i:nil="true"/>
      <a:LastName>Bowman</a:LastName>
      </a:Customer>
      </GetServiceRequestDetailsResult>




      I need to get the /ContractTransport/Value where the /ContractTransport/Name='Email'.



      The following xpath gets me to ContactTransport /*[local-name()='GetServiceRequestDetailsResponse']/*[local-name()='GetServiceRequestDetailsResult']/*[local-name()='Customer']/*[local-name()='ContactTransports']/*[local-name()='ContactTransport']



      If there were no namespaces then I guess I could suffix with [Name='Email]/*[local-name()='Value'] but this doesn't work now.










      share|improve this question














      There are lots of answers with no namespaces involved but I'm having trouble with the namespace.



      Here's the xml doc:



      <GetServiceRequestDetailsResponse xmlns="http://lcc.catscrm">
      <GetServiceRequestDetailsResult xmlns:a="http://schemas.datacontract.org/2004/07/LCC.CATS.CRM.WCF.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <a:AltRefs>
      <a:AltRefModel>
      <a:Name>UTRN</a:Name>
      <a:TypeId>7577</a:TypeId>
      <a:Value>9024930</a:Value>
      </a:AltRefModel>
      </a:AltRefs>
      <a:Customer>
      <a:Address>
      <a:Address1>5</a:Address1>
      <a:Address2>Random</a:Address2>
      <a:County>West Yorks</a:County>
      <a:NumberName>5</a:NumberName>
      <a:Postcode>L1</a:Postcode>
      <a:TownCity>London</a:TownCity>
      <a:UPRN i:nil="true"/>
      </a:Address>
      <a:AltRefs/>
      <a:ContactTransports>
      <a:ContactTransport>
      <a:Name>MobileNumber</a:Name>
      <a:TypeId>3</a:TypeId>
      <a:Value>08965</a:Value>
      </a:ContactTransport>
      <a:ContactTransport>
      <a:Name>Email</a:Name>
      <a:TypeId>1</a:TypeId>
      <a:Value>a@b.com</a:Value>
      </a:ContactTransport>
      </a:ContactTransports>
      <a:CustomerUPRN i:nil="true"/>
      <a:FirstName>Rob</a:FirstName>
      <a:Id i:nil="true"/>
      <a:LastName>Bowman</a:LastName>
      </a:Customer>
      </GetServiceRequestDetailsResult>




      I need to get the /ContractTransport/Value where the /ContractTransport/Name='Email'.



      The following xpath gets me to ContactTransport /*[local-name()='GetServiceRequestDetailsResponse']/*[local-name()='GetServiceRequestDetailsResult']/*[local-name()='Customer']/*[local-name()='ContactTransports']/*[local-name()='ContactTransport']



      If there were no namespaces then I guess I could suffix with [Name='Email]/*[local-name()='Value'] but this doesn't work now.







      xml xpath xpath-1.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 11:22









      Rob BowmanRob Bowman

      2,7051558112




      2,7051558112






















          2 Answers
          2






          active

          oldest

          votes


















          2














          You can add



          /*[local-name()="Value" and preceding-sibling::*[local-name()="Name" and .="Email"]]


          to your existed XPath to get required output






          share|improve this answer






























            2














            This might not be the most elegant option, but here's one:



            //*[local-name()='ContactTransport']/*[local-name()='Value' and ../*[local-name()='Name' and text()='Email']]


            This will select all Value elements underneath a ContactTransport that have a sibling whose Name text is Email. Demo.






            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%2f53318371%2fxpath-get-value-based-on-sibling-value-with-namespace%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You can add



              /*[local-name()="Value" and preceding-sibling::*[local-name()="Name" and .="Email"]]


              to your existed XPath to get required output






              share|improve this answer



























                2














                You can add



                /*[local-name()="Value" and preceding-sibling::*[local-name()="Name" and .="Email"]]


                to your existed XPath to get required output






                share|improve this answer

























                  2












                  2








                  2







                  You can add



                  /*[local-name()="Value" and preceding-sibling::*[local-name()="Name" and .="Email"]]


                  to your existed XPath to get required output






                  share|improve this answer













                  You can add



                  /*[local-name()="Value" and preceding-sibling::*[local-name()="Name" and .="Email"]]


                  to your existed XPath to get required output







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 12:08









                  AnderssonAndersson

                  39.1k113669




                  39.1k113669























                      2














                      This might not be the most elegant option, but here's one:



                      //*[local-name()='ContactTransport']/*[local-name()='Value' and ../*[local-name()='Name' and text()='Email']]


                      This will select all Value elements underneath a ContactTransport that have a sibling whose Name text is Email. Demo.






                      share|improve this answer



























                        2














                        This might not be the most elegant option, but here's one:



                        //*[local-name()='ContactTransport']/*[local-name()='Value' and ../*[local-name()='Name' and text()='Email']]


                        This will select all Value elements underneath a ContactTransport that have a sibling whose Name text is Email. Demo.






                        share|improve this answer

























                          2












                          2








                          2







                          This might not be the most elegant option, but here's one:



                          //*[local-name()='ContactTransport']/*[local-name()='Value' and ../*[local-name()='Name' and text()='Email']]


                          This will select all Value elements underneath a ContactTransport that have a sibling whose Name text is Email. Demo.






                          share|improve this answer













                          This might not be the most elegant option, but here's one:



                          //*[local-name()='ContactTransport']/*[local-name()='Value' and ../*[local-name()='Name' and text()='Email']]


                          This will select all Value elements underneath a ContactTransport that have a sibling whose Name text is Email. Demo.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 12:10









                          Robbie AverillRobbie Averill

                          21.2k74177




                          21.2k74177



























                              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%2f53318371%2fxpath-get-value-based-on-sibling-value-with-namespace%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

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo