Modify attribute if node exist with Xpath?











up vote
0
down vote

favorite












I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
xml file looks like that :



<krpano>
<hotspot name="hs1" ath="0" atv="0"/>
<hotspot name="hs2" ath="0" atv="0"/>
</krpano>


and here is my php code :



<?php
$str_json = file_get_contents('php://input');
$json_data = json_decode($str_json);

$file = 'myxmlfile.xml';
$xml = simplexml_load_file($file);
$krpano = $xml->xpath("//krpano");
$hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');

if ($hotspot){
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
}else{
$newhs = $krpano[0]->addChild('hotspot');
$newhs->addAttribute('name', $json_data->name);
$newhs->addAttribute('ath', $json_data->xpos);
$newhs->addAttribute('atv', $json_data->ypos);
}

$xml->asXML($file);

?>


if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
    xml file looks like that :



    <krpano>
    <hotspot name="hs1" ath="0" atv="0"/>
    <hotspot name="hs2" ath="0" atv="0"/>
    </krpano>


    and here is my php code :



    <?php
    $str_json = file_get_contents('php://input');
    $json_data = json_decode($str_json);

    $file = 'myxmlfile.xml';
    $xml = simplexml_load_file($file);
    $krpano = $xml->xpath("//krpano");
    $hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');

    if ($hotspot){
    $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
    $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
    }else{
    $newhs = $krpano[0]->addChild('hotspot');
    $newhs->addAttribute('name', $json_data->name);
    $newhs->addAttribute('ath', $json_data->xpos);
    $newhs->addAttribute('atv', $json_data->ypos);
    }

    $xml->asXML($file);

    ?>


    if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
      xml file looks like that :



      <krpano>
      <hotspot name="hs1" ath="0" atv="0"/>
      <hotspot name="hs2" ath="0" atv="0"/>
      </krpano>


      and here is my php code :



      <?php
      $str_json = file_get_contents('php://input');
      $json_data = json_decode($str_json);

      $file = 'myxmlfile.xml';
      $xml = simplexml_load_file($file);
      $krpano = $xml->xpath("//krpano");
      $hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');

      if ($hotspot){
      $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
      $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
      }else{
      $newhs = $krpano[0]->addChild('hotspot');
      $newhs->addAttribute('name', $json_data->name);
      $newhs->addAttribute('ath', $json_data->xpos);
      $newhs->addAttribute('atv', $json_data->ypos);
      }

      $xml->asXML($file);

      ?>


      if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.










      share|improve this question















      I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
      xml file looks like that :



      <krpano>
      <hotspot name="hs1" ath="0" atv="0"/>
      <hotspot name="hs2" ath="0" atv="0"/>
      </krpano>


      and here is my php code :



      <?php
      $str_json = file_get_contents('php://input');
      $json_data = json_decode($str_json);

      $file = 'myxmlfile.xml';
      $xml = simplexml_load_file($file);
      $krpano = $xml->xpath("//krpano");
      $hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');

      if ($hotspot){
      $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
      $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
      }else{
      $newhs = $krpano[0]->addChild('hotspot');
      $newhs->addAttribute('name', $json_data->name);
      $newhs->addAttribute('ath', $json_data->xpos);
      $newhs->addAttribute('atv', $json_data->ypos);
      }

      $xml->asXML($file);

      ?>


      if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.







      php xpath nodes simplexml






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:35









      executable

      808221




      808221










      asked Nov 8 at 11:14









      jeromebg

      62




      62
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can't alter attributes like that with XPath,



          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');


          you just need to modify them directly from the hotspot you already have...



          $hotspot[0]['ath'] = $json_data->xpos;
          $hotspot[0]['atv'] = $json_data->ypos;





          share|improve this answer





















          • GREAT !!! Thx a lot, you made my day ;)
            – jeromebg
            Nov 8 at 11:25










          • Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
            – Nigel Ren
            Nov 8 at 11:30











          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',
          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%2f53206619%2fmodify-attribute-if-node-exist-with-xpath%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can't alter attributes like that with XPath,



          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');


          you just need to modify them directly from the hotspot you already have...



          $hotspot[0]['ath'] = $json_data->xpos;
          $hotspot[0]['atv'] = $json_data->ypos;





          share|improve this answer





















          • GREAT !!! Thx a lot, you made my day ;)
            – jeromebg
            Nov 8 at 11:25










          • Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
            – Nigel Ren
            Nov 8 at 11:30















          up vote
          0
          down vote













          You can't alter attributes like that with XPath,



          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');


          you just need to modify them directly from the hotspot you already have...



          $hotspot[0]['ath'] = $json_data->xpos;
          $hotspot[0]['atv'] = $json_data->ypos;





          share|improve this answer





















          • GREAT !!! Thx a lot, you made my day ;)
            – jeromebg
            Nov 8 at 11:25










          • Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
            – Nigel Ren
            Nov 8 at 11:30













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can't alter attributes like that with XPath,



          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');


          you just need to modify them directly from the hotspot you already have...



          $hotspot[0]['ath'] = $json_data->xpos;
          $hotspot[0]['atv'] = $json_data->ypos;





          share|improve this answer












          You can't alter attributes like that with XPath,



          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
          $xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');


          you just need to modify them directly from the hotspot you already have...



          $hotspot[0]['ath'] = $json_data->xpos;
          $hotspot[0]['atv'] = $json_data->ypos;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:22









          Nigel Ren

          22.3k61832




          22.3k61832












          • GREAT !!! Thx a lot, you made my day ;)
            – jeromebg
            Nov 8 at 11:25










          • Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
            – Nigel Ren
            Nov 8 at 11:30


















          • GREAT !!! Thx a lot, you made my day ;)
            – jeromebg
            Nov 8 at 11:25










          • Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
            – Nigel Ren
            Nov 8 at 11:30
















          GREAT !!! Thx a lot, you made my day ;)
          – jeromebg
          Nov 8 at 11:25




          GREAT !!! Thx a lot, you made my day ;)
          – jeromebg
          Nov 8 at 11:25












          Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
          – Nigel Ren
          Nov 8 at 11:30




          Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
          – Nigel Ren
          Nov 8 at 11:30


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206619%2fmodify-attribute-if-node-exist-with-xpath%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

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff