doucha5080
doucha5080
2016-07-01 11:41

从XML节点获取数据

I am a beginner on PHP and XML.

I have a XML file as below (partly):

<combination>
  <id_combination>2289</id_combination>
  <quantity>4</quantity>
  <unit_price_impact>0.000000</unit_price_impact>
  <reference>K10100.1B</reference>
  <group_name>Color</group_name>
  <attribute_name>Blue</attribute_name>
</combination>

<combination>
  <id_combination>2289</id_combination>
  <quantity>4</quantity>
  <unit_price_impact>0.000000</unit_price_impact>
  <reference>K10100.1B</reference>
  <group_name>Size</group_name>
  <attribute_name>1</attribute_name>
</combination>

<combination>
  <id_combination>2290</id_combination>
  <quantity>20</quantity>
  <unit_price_impact>0.000000</unit_price_impact>
  <reference>K10100.2B</reference>
  <group_name>Color</group_name>
  <attribute_name>Blue</attribute_name>
</combination>

<combination>
  <id_combination>2290</id_combination>
  <quantity>20</quantity>
  <unit_price_impact>0.000000</unit_price_impact>
  <reference>K10100.2B</reference>
  <group_name>Size</group_name>
  <attribute_name>2</attribute_name>
</combination>

And i desire to get an array as described below:

$id_combination => 2289
$reference => K10100.1B
$combination_name => Color: Blue / Size: 1
$quantity => 4

$id_combination => 2290
$reference => K10100.2B
$combination_name => Color: Blue / Size: 2
$quantity => 20

I want to join data with the same 'id_combination' nodes and then process it in PHP.

I tried to use "foreach" loop, "array_unique", "implode" and etc.. but wasn't able to get any success.

I would appreciate anyone would help me straight to result with the suggested code.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongxi0523 dongxi0523 5年前

    Consider an XSLT solution as this is a typical need for the Muenchian Method in XSLT 1.0 used to group nodes by various keys. As information, XSLT is a special-purpose language designed to transform XML files and like most general purpose languages, PHP maintains an XSLT processor.

    XSLT Script (save as .xsl to be loaded in script below)

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>
    
    <xsl:key name="idkey" match="combination" use="id_combination" />
    
      <xsl:template match="combinations">
        <xsl:copy>
          <xsl:apply-templates select="combination[generate-id() =
                                       generate-id(key('idkey',id_combination)[1])]"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="combination[generate-id() =
                           generate-id(key('idkey',id_combination)[1])]">
        <xsl:copy>
          <xsl:copy-of select="id_combination|quantity|unit_price_impact|reference"/>
          <combination_name>
            <xsl:for-each select="key('idkey',id_combination)">
              <xsl:value-of select="concat(group_name, ': ', attribute_name)" />
                <xsl:if test="position() != last()">
                    <xsl:text> / </xsl:text>
                </xsl:if>               
            </xsl:for-each>
          </combination_name>
        </xsl:copy>
      </xsl:template>
    
    </xsl:transform>
    

    PHP Script

    // LOAD XML AND XSL FILES
    $xml = new DOMDocument('1.0', 'UTF-8');
    $xml->load('Input.xml');
    
    $xslfile = new DOMDocument('1.0', 'UTF-8');
    $xslfile->load('XSLTScript.xsl');
    
    // TRANSFORM XML with XSLT
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xslfile); 
    $newXml = $proc->transformToXML($xml);
    
    // ECHO OUTPUT STRING
    echo $newXml;
    
    $xml = new SimpleXMLElement($newXml);
    $xpath = $xml->xpath('//combination');
    
    $array = [];    
    foreach($xpath as $result){
        $inner = [];
        foreach ($result as $node => $item) {        
            $inner[$node] = (string)$item;
        }
        $array[] = $inner;
    }
    
    var_dump($array);
    

    Transformed XML

     <?xml version="1.0" encoding="UTF-8"?>
     <combinations>
       <combination>
         <id_combination>2289</id_combination>
         <quantity>4</quantity>
         <unit_price_impact>0.000000</unit_price_impact>
         <reference>K10100.1B</reference>
         <combination_name>Color: Blue / Size: 1</combination_name>
       </combination>
       <combination>
         <id_combination>2290</id_combination>
         <quantity>20</quantity>
         <unit_price_impact>0.000000</unit_price_impact>
         <reference>K10100.2B</reference>
         <combination_name>Color: Blue / Size: 2</combination_name>
       </combination>
     </combinations>
    

    Array Output

     array(2) {
       [0]=>
       array(5) {
         ["id_combination"]=>
         string(4) "2289"
         ["quantity"]=>
         string(1) "4"
         ["unit_price_impact"]=>
         string(8) "0.000000"
         ["reference"]=>
         string(9) "K10100.1B"
         ["combination_name"]=>
         string(21) "Color: Blue / Size: 1"
       }
       [1]=>
       array(5) {
         ["id_combination"]=>
         string(4) "2290"
         ["quantity"]=>
          string(2) "20"
         ["unit_price_impact"]=>
         string(8) "0.000000"
         ["reference"]=>
        string(9) "K10100.2B"
         ["combination_name"]=>
         string(21) "Color: Blue / Size: 2"
       }
     }
    
    点赞 评论 复制链接分享
  • douxiajia6309 douxiajia6309 5年前

    i just did the following changes to your xml and have append new code and look and worked on my xampp server

    $xml='<?xml version="1.0" encoding="UTF8"?>
            <combinations>
                <combination>
                  <id_combination>2289</id_combination>
                  <quantity>4</quantity>
                  <unit_price_impact>0.000000</unit_price_impact>
                  <reference>K10100.1B</reference>
                  <group_name>Color</group_name>
                  <attribute_name>Blue</attribute_name>
                </combination>
    
                <combination>
                  <id_combination>2289</id_combination>
                  <quantity>4</quantity>
                  <unit_price_impact>0.000000</unit_price_impact>
                  <reference>K10100.1B</reference>
                  <group_name>Size</group_name>
                  <attribute_name>1</attribute_name>
                </combination>
    
                <combination>
                  <id_combination>2290</id_combination>
                  <quantity>20</quantity>
                  <unit_price_impact>0.000000</unit_price_impact>
                  <reference>K10100.2B</reference>
                  <group_name>Color</group_name>
                  <attribute_name>Blue</attribute_name>
                </combination>
    
                <combination>
                  <id_combination>2290</id_combination>
                  <quantity>20</quantity>
                  <unit_price_impact>0.000000</unit_price_impact>
                  <reference>K10100.2B</reference>
                  <group_name>Size</group_name>
                  <attribute_name>2</attribute_name>
                </combination>
            </combinations>';
            $xmlobject = simplexml_load_string($xml);
            echo '<pre>';
            // this print an array of objects
            print_r($xmlobject);
            // this print the associative array
            print_r((array)$xmlobject);
    
    点赞 评论 复制链接分享