SimpleXML 根据字段获取特定值
SimpleXML get certain value based on a field
我正在尝试根据 PRODUCT_NUMBER.
获取 PRINT_POSITION_URL 的 url
对于产品编号为 7375-06 的产品,我想获得 PRINT_POSITION_URL 颜色="06",对于产品产品编号 7375-04 我想得到 PRINT_POSITION_URL color="04" 等
这两个值位于不同的 XML 文件中,我从 url 中获取它们,并且它们与字段 PRODUCT_PRINT_ID 相关。
这是我的第一个 XML 文件(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>
这是我的第二个 XML 文件(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>
这是我试过的方法:
<?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();
?>
结果如下:
<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>
在您的实际 xml:
上尝试变体
<?php
$stringA = <<<XML
<PRODUCTS>
<PRODUCT>
<PRODUCT_NUMBER>7375-06</PRODUCT_NUMBER>
<PRODUCT_NAME>Soft ball</PRODUCT_NAME>
</PRODUCT>
</PRODUCTS>
XML;
$xmlA = simplexml_load_string($stringA);
$resultA = $xmlA->xpath("//PRODUCTS//PRODUCT");
$num = $xmlA->xpath("//PRODUCT//PRODUCT_NUMBER/.");
$tnum = implode($num);
$stringB = <<<XML
<PRINTINGINFORMATION>
<PRODUCTS>
<PRODUCT>
<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>
XML;
$xmlB = simplexml_load_string($stringB);
$resultB = $xmlB->xpath("//PRODUCT//PRINT_POSITION_URL[@color=substring-after('{$tnum}','-')]/text()");
// create empty output xml object
$final = new simpleXMLElement('<?xml version="1.0" encoding="utf-8"?><PRODUCTINFORMATION></PRODUCTINFORMATION>');
$products = $final->addChild("PRODUCTS");
foreach ($resultA as $proda) {
$prodbaseno = (string)$proda->PRODUCT_NUMBER;
$prodname = (string)$proda->PRODUCT_NAME;
// build the output xml
$prodnew = $products->addChild('PRODUCT');
$prodnew->addChild('PRODUCT_NUMBER', $prodbaseno);
$prodnew->addChild('PRODUCT_NAME', $prodname);
foreach ($resultB as $prtpos ) {
$prodnew->addChild('PRINT_POSITION_URL', $prtpos);
}
}
echo $final->saveXml();
?>
输出就是你的问题。
我正在尝试根据 PRODUCT_NUMBER.
获取 PRINT_POSITION_URL 的 url对于产品编号为 7375-06 的产品,我想获得 PRINT_POSITION_URL 颜色="06",对于产品产品编号 7375-04 我想得到 PRINT_POSITION_URL color="04" 等
这两个值位于不同的 XML 文件中,我从 url 中获取它们,并且它们与字段 PRODUCT_PRINT_ID 相关。
这是我的第一个 XML 文件(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>
这是我的第二个 XML 文件(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>
这是我试过的方法:
<?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();
?>
结果如下:
<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>
在您的实际 xml:
上尝试变体<?php
$stringA = <<<XML
<PRODUCTS>
<PRODUCT>
<PRODUCT_NUMBER>7375-06</PRODUCT_NUMBER>
<PRODUCT_NAME>Soft ball</PRODUCT_NAME>
</PRODUCT>
</PRODUCTS>
XML;
$xmlA = simplexml_load_string($stringA);
$resultA = $xmlA->xpath("//PRODUCTS//PRODUCT");
$num = $xmlA->xpath("//PRODUCT//PRODUCT_NUMBER/.");
$tnum = implode($num);
$stringB = <<<XML
<PRINTINGINFORMATION>
<PRODUCTS>
<PRODUCT>
<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>
XML;
$xmlB = simplexml_load_string($stringB);
$resultB = $xmlB->xpath("//PRODUCT//PRINT_POSITION_URL[@color=substring-after('{$tnum}','-')]/text()");
// create empty output xml object
$final = new simpleXMLElement('<?xml version="1.0" encoding="utf-8"?><PRODUCTINFORMATION></PRODUCTINFORMATION>');
$products = $final->addChild("PRODUCTS");
foreach ($resultA as $proda) {
$prodbaseno = (string)$proda->PRODUCT_NUMBER;
$prodname = (string)$proda->PRODUCT_NAME;
// build the output xml
$prodnew = $products->addChild('PRODUCT');
$prodnew->addChild('PRODUCT_NUMBER', $prodbaseno);
$prodnew->addChild('PRODUCT_NAME', $prodname);
foreach ($resultB as $prtpos ) {
$prodnew->addChild('PRINT_POSITION_URL', $prtpos);
}
}
echo $final->saveXml();
?>
输出就是你的问题。