【问题标题】:XML processing, ordering and loopingXML 处理、排序和循环
【发布时间】:2017-06-28 06:43:39
【问题描述】:

我已经为渲染 XML 提要苦苦挣扎了 10 天:

http://feed.harjbains.gnomen-europe.com/xml-feed/

我尝试过simplexmlxmlDOM 和 XSLT 都有细微差别。我的编程技能目前有点挑战,我不知道是我的医生给我的药物还是我只是变老了,应该放弃黑客代码。

我研究过多维数组、JSON 和 XML 样式表,XSLT 非常繁琐,不喜欢我的 XML 并且会干扰我的样式表。

我希望能够通过显示 date_updatedprice 顺序来呈现 XML,并通过 status 限制结果并能够将记录数限制为 n

这是我将每个元素放入数组的代码,但是我无法进行这样的排序。

<?php   
    $xml = simplexml_load_file("http://feed.harjbains.gnomen-europe.com/xml-feed/");

    $properties = $xml;

    $id             = array();
    $beds           = array();
    $baths          = array();
    $price          = array();
    $address1       = array();
    $area           = array();
    $postcode       = array();
    $date_updated   = array();
    $transaction    = array();
    $status         = array();
    $image          = array();
    $img            = array();
    $description    = array();

    $i=0;
    $totrecs=0;
    foreach ($properties as $property) {
        $i=$i+1;
        $address[$i]      = $property->address1;
        $id[$i]           = $property->id;
        $beds[$i]         = $property->beds;
        $baths[$i]        = $property->baths;
        $price[$i]        = $property->price;
        $address1[$i]     = $property->address1;
        $area[$i]         = $property->area;
        $postcode[$i]     = $property->postcode;
        $date_updated[$i] = $property->date_updated;
        $transaction[$i]  = $property->transaction;
        $status[$i]       = $property->status;
        $image[$i]        = $property->image;
        $description[$i]  = $property->description;
        $totrecs          = count($id);

        if(isset($property->image->img)) {
            //echo $address1[$i]."<BR>";
            $image[$i] = true;
            $x=0;
            foreach($property->image->img as $a) {
                $img[$i][$x]= $a;
                //echo $img[$i][$x]."<BR>";
                $x=$x+1;
            }
        } else {
           $image[$i] = false;
        }
    }
?> 

哈吉

【问题讨论】:

    标签: php arrays xml xslt xmldom


    【解决方案1】:

    重新考虑 XSLT,因为它维护了一个 sort 方法。为了演示,下面将原始 XML 提要转换为具有按价格降序排列的属性的 XML,仅选择特定元素,过滤到前 5 个(&amp;lt; 相当于 &lt;)并且仅具有 status='Let '.

    XSLT (另存为 .xsl 文件或嵌入的 PHP 字符串)

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes" />
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/properties">
         <xsl:copy>
           <xsl:apply-templates select="property[position() &lt; 6 and status='Let']">
                <xsl:sort select="translate(price, ',', '')" data-type="number" order="descending"/>
           </xsl:apply-templates>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="property">
      <xsl:copy>
         <xsl:copy-of select="id|beds|baths|price|address1|area|postcode|date_updated|transaction|status|image|img|description"/>
      </xsl:copy>
     </xsl:template>
    
    </xsl:stylesheet>
    

    PHP (确保在 .ini 文件中启用 php-xsl 扩展)

    <?php
    
    // LOAD XML FEED
    $xml = simplexml_load_file("http://feed.harjbains.gnomen-europe.com/xml-feed/");
    
    // LOAD XSLT SCRIPT
    $xsl = new DOMDocument;
    $xsl->load('XSLT_Script.xsl');   // OR $xsl->loadXML($xslstring);
    
    // INITIALIZE TRANSFORMER
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl);    
    
    // TRANSFORM SOURCE TO NEW TREE
    $newXML = $proc->transformToXML($xml);
    
    // ECHO OUTPUT
    echo $newXML;
    
    // SAVE OUTPUT TO FILE
    file_put_contents('Output.xml', $newXML);
    
    ?>
    

    输出

    <properties>
        <property>
            <id>1184</id>
            <beds>3</beds>
            <baths>3</baths>
            <image>
            </image>
            <price>695</price>
            <area>Wolverhampton</area>
            <address1>Park Hall Road</address1>
            <description>A vastly extended three bedroom semi-detached property. Just been totally re-furbished. Comprising of front reception room, extended through lounge, third room which can be used as fourth bedroom. This leads into a shower room which has WC and basin. Utility room with plumbing. Fitted kitchen with gas cooker and hob. To the first floor, master bedroom with en-suite. Two further double bedrooms and family bathroom. Rear garden and front garden with drive for off-road parking.</description>
            <postcode>WV4 5DU</postcode>
            <date_updated></date_updated>
            <transaction>2</transaction>
            <status>Let</status>
        </property>
        <property>
            <id>1176</id>
            <beds>3</beds>
            <baths>1</baths>
            <image>
            </image>
            <price>575</price>
            <area>Wolverhampton</area>
            <address1>Park Hall Road</address1>
            <description>A very well presented three bedroom semi-detached property which has been totally re-furbished. Living room, leading to dining room and fitted kitchen with cooker. Three bedrooms and new family bathroom with shower. New flooring and carpets throughout. Double glazed and gas central heated. Rear garden with patio, front garden with drive leading to garage. Available immediately.</description>
            <postcode>WV4 5DU</postcode>
            <date_updated></date_updated>
            <transaction>2</transaction>
            <status>Let</status>
        </property>
        <property>
            <id>1181</id>
            <beds>3</beds>
            <baths>1</baths>
            <image>
            </image>
            <price>550</price>
            <area>Willenhall</area>
            <address1>Westfield Road</address1>
            <description>A very well presented three bedroom semi-detached property. Front reception room, large fitted kitchen with dining area. To the first floor, three bedrooms and modern family bathroom with separate shower cubicle. Front garden with space for off-road parking, rear garden. Double glazed and central heated throughout. Available immediately.</description>
            <postcode>WV13 3JX</postcode>
            <date_updated></date_updated>
            <transaction>2</transaction>
            <status>Let</status>
        </property>
        <property>
            <id>1174</id>
            <beds>3</beds>
            <baths>1</baths>
            <image>
            </image>
            <price>550</price>
            <area>Willenhall</area>
            <address1>Sandringham Avenue</address1>
            <description>Three bedroom semi-detached property in popular residential area. Comprising of through lounge, fitted kitchen. Patio door to rear garden. Three bedrooms to first floor. Family bathroom with shower. Garage to side, front garden and off-road parking. Available immediately.</description>
            <postcode>WV12 5TF</postcode>
            <date_updated></date_updated>
            <transaction>2</transaction>
            <status>Let</status>
        </property>
        <property>
            <id>1177</id>
            <beds>3</beds>
            <baths>1</baths>
            <image>
            </image>
            <price>500</price>
            <area>Willenhall</area>
            <address1>Marshall Road</address1>
            <description>Three bedroom semi-detached property. Comprising of two reception rooms, fitted kitchen to ground floor. Three bedrooms and family bathroom to first floor. Double glazed and central heated throughout. Front and rear gardens. Available immediately.</description>
            <postcode>WV13 3PB</postcode>
            <date_updated></date_updated>
            <transaction>2</transaction>
            <status>Let</status>
        </property>
    </properties>
    

    【讨论】:

      猜你喜欢
      • 2015-04-05
      • 2023-03-23
      • 2012-01-07
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 2016-10-21
      • 1970-01-01
      相关资源
      最近更新 更多