【问题标题】:Get XML element based on attribute value with php from multiple xmlfeeds使用来自多个 xmlfeed 的 php 根据属性值获取 XML 元素
【发布时间】:2012-02-08 18:33:00
【问题描述】:

首先,我有一个首页,其中包含来自多个外部 xml 提要的多个产品(目前 24 个不同的提要),每个提要 1 个产品。有些属性对同一事物有不同的名称,例如:

一个示例供稿

<products>
<product>
<productID>4943</productID>
<name>Inbouwspot</name>
<price currency="EUR">199.00</price>
<productURL>http://www.example.com</productURL>
<imageURL>http://static.example.com/45928-1.jpg</imageURL>
<description><![CDATA[Incl. BTW!! Verkrijgbaar in een voordeelset.]]></description>
<categories>
<category path="Aanbiedingen">Aanbiedingen</category>
<category path="Projectverlichting">Projectverlichting</category>
</categories>
<additional>
<field name="fromPrice">395.00</field>
<field name="discount">196.00</field>
<field name="deliveryCosts"></field>
<field name="deliveryTime">3-5 dagen</field>
</additional>
</product>

另一个示例供稿

<products>
<product>
<productID>1344</productID>
<name>Displex screen</name>
<price currency="EUR">6.95</price>
<productURL>http://www.voorbeeld.nl</productURL>
<imageURL>http://www.voorbeeld.nl/images/1543.jpg</imageURL>
<description><![CDATA[Touchscreen voor je smartphone?]]></description>
<categories>
</categories>
<additional>
<field name="price_from">14.95</field>
<field name="price_shipping">Gratis!</field>
<field name="soldout">0</field>
<field name="startdate">2012-02-07 12:00:00</field>
<field name="enddate">2012-02-08 12:00:00</field>
</additional>
</product>
</products>

我在这样的循环中呈现的不同提要(产品):

<?php
    // get xml file contents
    $xmlurl = get_post_meta( get_the_ID( ), 'plus_xmlfeed', true );
    if ( !empty( $xmlurl ) ){
    $xml = simplexml_load_file($xmlurl);
    }
    $xml->product->name = substr($xml->product->name, 0, 30).'...';
    $desc = substr($xml->product->name, 0, 50).'';
?>

    <li class="span3">
      <div class="thumbnail">
      <a href="<?php echo $xml->product->productURL ;?>">
          <center><img src="<?php echo get_template_directory_uri(); ?>/img/logos/<?php echo get_post_meta(get_the_ID( ), 'plus_logos', true); ?>.gif"/></center>
          <center><img class="mainimg" alt="" src="<?php echo $xml->product->imageURL ;?>"/></center>
        </a>
        <div class="caption">
          <h5><a href="<?php echo $xml->product->productURL ;?>"><?php echo $xml->product->name; ?></a></h5>
          <div class="maindesc half clearfix"><div class="prijs">&euro; <?php echo $xml->product->price ;?></div><strike style="color: rgb(248, 148, 6);">
            <?php
            foreach ($xml->product->additional->field as $field) {
                               switch((string) $field['name']) {
                                   case 'price_advice':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'fromPrice':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'oldPrice':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'from_price':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'adviesprijs':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'advice_price':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'Fromprice':
                                      echo '&euro; ' .$field. '';
                                      break;
                                   case 'recommendedPrice':
                                      echo '&euro; ' .$field. '';
                                      break;
                                    }
                                }
            ?>
            </strike>
          <p style="color: rgb(153, 153, 153);"><?php echo $desc; ?></p></div>
          <div class="btn-group bot"><div class="pull-right"> </div><a class="btn btn-primary" href="<?php echo $xml->product->productURL ;?>">Kopen</a> <a class="btn" href="#<?php echo 'modal_'.get_the_ID();?>">Info</a></div>
        </div>
      </div>

    </li>
<?php /*end loop*/ ?>

我正在使用 wordpress 在帖子中输入提要,并将其显示为变量 ($xmlurl)。我还想在帖子中输入特定属性(fromPricefrom_price 等),这样它就不必循环遍历所有选项来找到适合该提要的选项.

我知道上面可以更快,更有效,我希望有人可以帮助我。我读了一些关于 xpath 的东西,我尝试过但没有成功。

【问题讨论】:

    标签: php xml xpath


    【解决方案1】:

    我自己的解决方案:

    我自己找到了解决方案

                <?php
                    $FromPrices= get_post_meta(get_the_ID( ), 'plus_fromprices', true);
                    if ( !empty( $FromPrices) ){
                    foreach ($xml->xpath('//field[@name="'.$FromPrices.'"]') as $fromprice) {
                        echo '&euro; '.$fromprice;
                        }
                    }
                ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2011-05-07
      相关资源
      最近更新 更多