从XML中的嵌套属性节点中选择和读取值

时间:2016-07-13 14:36:34

标签: php xml xmlwriter

我有一个PHP脚本,它读取XML文件并将其转换为我的XML格式。原始XML看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <products>
    <product>
      <id>1</id>
      <stock>23</stock>
      ... 6 more nodes....
      <pictures>
        <pic1>linktopic1.jpg</pic1>
        <pic2>linktopic2.jpg</pic2>
      </pictures>
      <tax_percent>...</tax_percent>
      <price_dc>....</price_dc>
      ...... some more nodes....
      <attributes>
        <attribute name="Sample attribute" value="Sample value" />
        <attribute name="Sample attribute 2" value="Sample value 2"/>
        .... 10+ attributes for each product....
      </attributes>

    </product>
   </products>
 </root>

我正在使用XMLwriter来读取XML并写入新的XML。位于不同节点中的所有图片和属性必须在一个字符串中。我设法为图片做了这个,但它不适用于属性。

foreach($xml->children() as $products) { 
  foreach($products->children() as $product) {
      $newXML->startElement("product");
      $newXML->writeElement("ID", $product->id);
      $newXML->writeElement("Stock", $product->stock);
      $pic=""; // string resets for each product node
      $atr=""; // Attribute string
         foreach($product->pictures as $child) { // pictures
           if ($child->pic1!="") { $pic=$pic.$child->pic1.","; } // If the picture subnode exists it appends the image to previous ones 
           if ($child->pic2!="") { $pic=$pic.$child->pic2.","; }
           if ($child->pic3!="") { $pic=$pic.$child->pic3.","; }
         }
         foreach($product->attributes as $child) { //attributes
           if ($child->attribute['name']=="Sample attribute") { $atr=$atr.$child->attribute['name'].':'.$child->attribute['value'].','; }
           if ($child->attribute['name']=="Sample attribute 2") { $atr=$atr.$child->attribute['name'].':'.$child->attribute['value'].','; }
         }
        ..... some more spaghetti code....

就像我说的那样,图片的代码是有效的,但由于某些原因,属性代码只会将产品的第一个属性写入字符串并跳过所有其他属性。

任何人都知道为什么foreach循环会跳过所有其他属性节点?对我而言,它看起来有某种问题,因为属性节点具有相同的节点名称,而图片具有动态名称,idk ..

1 个答案:

答案 0 :(得分:1)

考虑进一步深入迭代<attribute>标签:

foreach($product->attributes->attribute as $child) { //attributes
   if ($child['name']=="Sample attribute") { $atr=$atr.$child['name'].': '.$child['value'].','; }
   if ($child['name']=="Sample attribute 2") { $atr=$atr.$child['name'].': '.$child['value'].','; }
}

事实上,您甚至不需要if声明:

foreach($product->attributes->attribute as $child) { //attributes
   $atr=$atr.$child['name'].': '.$child['value'].',';
}

或者,由于您需要将源XML转换为最终XML,请考虑XSLT,这是专门用于转换XML文档的专用语言。在.ini文件中启用扩展的PHP( extension = php_xsl.so extension = php_xsl.dll )维护一个XSLT 1.0处理器。 XSLT下面连接图片和属性的子值(可扩展到10+节点):

XSLT (另存为.xsl文件,将在PHP下面加载)

<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:template match="products">
    <xsl:copy>
      <xsl:apply-templates select="product"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="product">
    <xsl:copy>
      <xsl:copy-of select="id|stock"/>
      <pictures>
        <xsl:for-each select="pictures">
          <xsl:value-of select="concat(pic1, ', ', pic2)" />
            <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
            </xsl:if>           
        </xsl:for-each>
      </pictures>
      <attributes>
        <xsl:for-each select="attributes/attribute">
          <xsl:value-of select="concat(@name, ': ', @value)" />
            <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
            </xsl:if>           
        </xsl:for-each>        
      </attributes>      
    </xsl:copy>
  </xsl:template>

</xsl:transform>

PHP (没有循环,没有if语句,没有局部变量,没有意大利面条代码)

// LOAD XML AND XSL SOURCES
$xml = simplexml_load_file('Input.xml');    
$xsl = simplexml_load_file('XSLTScript.xsl');

// CONFIGURE TRANSFORMER
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$newXML = $proc->transformToXML($xml);

echo $newXML;

// <?xml version="1.0" encoding="UTF-8"?>
// <products>
//   <product>
//     <id>1</id>
//     <stock>23</stock>
//     <pictures>linktopic1.jpg, linktopic2.jpg</pictures>
//     <attributes>Sample attribute: Sample value, Sample attribute 2: Sample value 2</attributes>
//   </product>
// </products>
相关问题