使用XPath搜索XML以获取“搜索结果”页面的多个值

时间:2017-02-02 19:12:27

标签: php xml xpath

我正在尝试使用XPath代码搜索XML:

$results = $xml->xpath("Property/Floorplan[SquareFeet / @Min>='".$minarea."' or EffectiveRent / @Min>='".$minprice."' or Room[@RoomType='Bathroom'] / Count>='".$minbath."' or Room[@RoomType='Bedroom'] / Count>='".$minbeds."']/parent::*");

它没有按预期工作。我正在寻找的是,它应该查看XML中为SquareFeet中的变量设置的最小值,EffectiveRent,Room>卧室和浴室。
例如:

  • $ minarea = 2000,
  • $ minprice = 500,
  • $ minbath = 2,
  • $ minbeds = 2。

代码当前返回所有节点而没有匹配。 另外,我想添加更多功能来搜索city,state和zip。

这是我的XML代码:

<PhysicalProperty xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Property>
        <PropertyID>
            <Address AddressType="property">
                <Address>518 Cedar St</Address>
                <City>Monticello</City>
                <State>MN</State>
                <PostalCode>55362</PostalCode>
            </Address>
        </PropertyID>
        <Floorplan>
            <Room RoomType="Bedroom">
                <Count>0</Count>
            </Room>
            <Room RoomType="Bathroom">
                <Count>0</Count>
            </Room>
            <SquareFeet Min="1680" Max="1680"/>
            <MarketRent Min="0" Max="0"/>
            <EffectiveRent Min="1295" Max="1295"/>
        </Floorplan>
    </Property>
</PhysicalProperty>

这是完整的PHP代码:

$all_active = get_template_directory().'/propertxml/propertyall.xml';
$xml=simplexml_load_file($all_active);
$results = $xml->xpath("Property/Floorplan[SquareFeet / @Min>=".$minarea." and EffectiveRent / @Min>=".$minprice." and Room[@RoomType='Bathroom'] / Count>=".$minbath." and Room[@RoomType='Bedroom'] / Count>=".$minbeds."]/parent::*"); 
if(!empty($results))
{
foreach($results as $res7){

$tt = $res7->PropertyID[0]->MarketingName;
$desc=$res7->Information->LongDescription;
$rent = $res7->Floorplan->EffectiveRent->attributes()->Min;
$area=$res7->Floorplan->SquareFeet->attributes()->Min ;
$bed=$res7->Floorplan->Room[0]->Count;
$bath = $res7->Floorplan->Room[1]->Count;
$url = $res7->Floorplan->FloorplanAvailabilityURL; 
$address = $res7->PropertyID[0]->Address->Address;
$unitcount = $res7->Information->UnitCount;
$rent = $res7->Floorplan->EffectiveRent->attributes()->Min;
$ptype=$res7->ILS_Identification->attributes()->ILS_IdentificationType; }

0 个答案:

没有答案