douyue7408 2016-07-29 00:28
浏览 80

从子元素中的XML获取自定义属性

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<Entries>
<Category name="Gallery Wrap">
<Entry>
  <Status>Active</Status>
  <ProductId>12x18_12framewrapwalnut</ProductId>
  <Size>
    <Height>12</Height>
    <Width>18</Width>
    <Depth>12</Depth>
  </Size>
  <Options>
    <Frame cost="86" id="12x18_12framewrapwalnut">Canvas</Frame>
  </Options>
  <Description>12x18 Walnut Framed Gallery Wrap</Description>
  <Weight>.1</Weight>
</Entry>
....
</Category>
</Entries>

I am trying to get the custom attributes for the element. Here is my PHP code so far:

$products = simplexml_load_file( 'product_list.xml' );

foreach ( $products as $category ) {
    $attributes = $category->attributes();
    echo '<h2>' . $attributes['name'] . '</h2>';

    echo '<table>';
    echo '<tr><th></th><th>Name</th><th>Your Cost</th><th>Set Price</th></tr>';

    foreach ( $category->Entry as $product ) {
        foreach ( $product->Options as $option ) {
            $option_attributes = $option->attributes();
            $option_vars = get_object_vars( $option );
            foreach ( $option_vars as $option_name => $option_value ) {
                echo '<tr><td><input type="checkbox" value="' . $option_attributes->id . '" /></td><td>' . $product->Description . ' - ' . $option_name . '</td><td>' . $option_attributes->cost . '</td><td><input type="text" size="5" value="' . $option_attributes->cost . '" data-cost="' . $option_attributes->cost . '" class="price" /></td></tr>';
            }
        }
    }
    echo '</table>';
}

I am building a form that takes this XML data let's users which products they want to include, setting their own price (all that aspect I can deal with later, just can't extract that "cost" and "id" attributes).

  • 写回答

1条回答 默认 最新

  • doutui8842 2016-07-29 03:12
    关注

    Consider an XSLT solution to transform your XML source to HTML. Here, you avoid any foreach loop. Below runs XSLT as an embedded string but can also be loaded from file like any other well-formed XML file. Be sure to enable the XSLT extension in php .ini file: extension=php_xsl.dll/php_xsl.so.

    // Load XML source
    $xml = new DOMDocument('1.0', 'UTF-8');
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->load('Input.xml');
    
    // Load XSLT string
    $xsl = new DOMDocument;
    $xslstr = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
                            omit-xml-declaration="yes"/>
    
                <xsl:template match="Entries">
                    <html>
                        <xsl:apply-templates select="Category"/>
                    </html>
                </xsl:template>
    
                <xsl:template match="Category">
                    <h2><xsl:value-of select="@name"/></h2>
                    <table>
                        <tr><th></th><th>Name</th><th>Description</th><th>Set-Price</th></tr>
                        <tr><xsl:apply-templates select="Entry"/></tr>
                    </table>
                </xsl:template>
    
                <xsl:template match="Entry">                
                    <td><input type="checkbox">
                        <xsl:attribute name="value"><xsl:value-of select="Options/Frame/@id"/></xsl:attribute>
                    </input></td>
                    <td><xsl:value-of select="Description"/>-<xsl:value-of select="ancestor::Category/@name"/></td>
                    <td><xsl:value-of select="Options/Frame/@cost"/></td>
                    <td>
                        <input type="text" size="5">
                            <xsl:attribute name="value"><xsl:value-of select="Options/Frame/@cost"/></xsl:attribute>
                            <xsl:attribute name="data-cost"><xsl:value-of select="Options/Frame/@cost"/></xsl:attribute>
                            <xsl:attribute name="class">price</xsl:attribute>
                        </input>
                    </td>               
    
                </xsl:template>
    
                </xsl:stylesheet>';
    $xsl->loadXML($xslstr);
    
    // Configure the transformer
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl);
    
    // Transform XML source
    $newXml = $proc->transformToXML($xml);
    echo $newXml;                                   // STRING VALUE
    

    Output

    <html>
      <h2>Gallery Wrap</h2>
      <table>
        <tr>
          <th/>
          <th>Name</th>
          <th>Your Cost</th>
          <th>Set-Price</th>
        </tr>
        <tr>
          <td>
            <input type="checkbox" value="12x18_12framewrapwalnut"/>
          </td>
          <td>12x18 Walnut Framed Gallery Wrap-Gallery Wrap</td>
          <td>86</td>
          <td>
            <input type="text" size="5" value="86" data-cost="86" class="price"/>
          </td>
        </tr>
      </table>
    </html>
    

    HTML Output from PHP Script

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错