Jump to content

Recommended Posts

I'm trying to get the url of PRINT_POSITION_URL based on PRODUCT_NUMBER.

For product with product number 7375-06 i want to get PRINT_POSITION_URL color="06", for product with product number 7375-04 i want to get PRINT_POSITION_URL color="04" etc.

These two values are in different XML files which i am getting them from url and they are related with a field PRODUCT_PRINT_ID.

Here is my first XML file(products.xml):

<PRODUCTS>
  <PRODUCT>
    <PRODUCT_NUMBER>7375-06</PRODUCT_NUMBER>
    <PRODUCT_NAME>Soft ball</PRODUCT_NAME>
    <PRODUCT_PRINT_ID>40002010</PRODUCT_PRINT_ID>
  </PRODUCT>
</PRODUCTS>

And here is my second XML file(print-info.xml):

<PRINTINGINFORMATION>
  <PRODUCTS>
    <PRODUCT>
     <PRODUCT_PRINT_ID>40002010</PRODUCT_PRINT_ID>
      <PRINTING_POSITIONS>
        <PRINTING_POSITION>
          <PRINT_POSITION_URL color="04">https://thumb_7375_04.jpg</PRINT_POSITION_URL>
          <PRINT_POSITION_URL color="05">https://thumb_7375_05.jpg</PRINT_POSITION_URL>
          <PRINT_POSITION_URL color="06">https://thumb_7375_06.jpg</PRINT_POSITION_URL>
        </PRINTING_POSITION>
      </PRINTING_POSITIONS>
    </PRODUCT>
  </PRODUCTS>
</PRINTINGINFORMATION>

Here is what I've tried based on @Barand answer in another topic and i want to thank him for his help.

<?php
header ("Content-Type:text/xml");
$xmlA = simplexml_load_file('ftp://.../prodinfo_EN.xml');
$xmlB = simplexml_load_file('ftp://.../printinfo.xml');

// create empty output xml object
$final = new simpleXMLElement('<?xml version="1.0" encoding="utf-8"?><PRODUCTINFORMATION></PRODUCTINFORMATION>'); 
$products = $final->addChild("PRODUCTS");

  foreach ($xmlA->PRODUCTS->PRODUCT as $proda) {
    $prodbaseno = (string)$proda->PRODUCT_NUMBER;
    $prodname = (string)$proda->PRODUCT_NAME;
    $prodprintid = (string)$proda->PRODUCT_PRINT_ID;

    // build the output xml
    $prodnew = $products->addChild('PRODUCT');
    $prodnew->addChild('PRODUCT_NUMBER', $prodbaseno);
    $prodnew->addChild('PRODUCT_NAME', $prodname);
    $prodnew->addChild('PRODUCT_PRINT_ID', $prodprintid);

    // find related field from xml file B based on PRODUCT_PRINT_ID
    if ($prodarr = $xmlB->xpath("PRODUCTS/PRODUCT[PRODUCT_PRINT_ID='$prodprintid']")) {
        $prodb = $prodarr[0];

        $prtposns = $prodnew->addChild('PRINTING_POSITIONS');
        foreach ($prodb->PRINTING_POSITIONS->PRINTING_POSITION as $prtpos )   {
            $posnew = $prtposns->addChild('PRINTING_POSITION');
            $posnew->addChild('PRINT_POSITION_URL', $prtpos->PRINT_POSITION_URL);
        }
    }

  }

echo $final->saveXml();
?>

And here is the output:

<PRODUCTINFORMATION>
  <PRODUCTS>
    <PRODUCT>
      <PRODUCT_NUMBER>MO7375-06</PRODUCT_NUMBER>
      <PRODUCT_NAME>Soft ball</PRODUCT_NAME>
      <PRODUCT_PRINT_ID>40002010</PRODUCT_PRINT_ID>
        <PRINTING_POSITIONS>
          <PRINTING_POSITION>
            <PRINT_POSITION_URL color="04">https://thumb_7375_04.jpg</PRINT_POSITION_URL>
          </PRINTING_POSITION>
        </PRINTING_POSITIONS>
    </PRODUCT>
  </PRODUCTS>
</PRODUCTINFORMATION>

As you can see I'm getting the PRINT_POSITION_URL for color="04" but i want PRINT_POSITION_URL for color="06" based on PRODUCT_NUMBER.

I should probably use xpath with substring-after method but i can't get it work.

Any help would be greatly appreciated.

I can't get the right results with the following.

$xmlB->xpath("//PRODUCT//PRINT_POSITION_URL[@color=substring-after('{$tnum}','-')]/text()");

I tried this in my first foreach with no luck.

$resultA = $xmlA->xpath("//PRODUCTS//PRODUCT");
$num = $xmlA->xpath("//PRODUCT//PRODUCT_NUMBER/.");
$tnum = implode($num);
$resultB = $xmlB->xpath("//PRODUCT//PRINT_POSITION_URL[@color=substring-after('{$tnum}','-')]/text()");

foreach ($resultB as $prtpos )   {
    $prodnew->addChild('PRINT_POSITION_URL', $prtpos);
}

 

Edited by Billy-3k

After trying to find for a solution i found out that

$resultB = $xmlB->xpath("//PRODUCT//PRINT_POSITION_URL[@color=substring-after('{$tnum}','-')]/text()");

isn't working. If i replace

 [@color=substring-after('{$tnum}','-')] 

with the product number for example  

[@color=substring-after('MO7375-06','-')]

outputs the right result. Any thoughts?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.