2011-08-11 8 views
1

PHPの初心者を助けてくれることを願っています。PHPのXPathを使用してXMLデータを(名前空間を使用して)フィルタリングする

PHP変数に基づいてXMLデータをフィルタリングする必要があります。ここに私のXMLファイルのカットです:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<rss version="2.0" xmlns:job="https://my/namespace/URL.dtd"> 
<channel> 
<title>search form results</title> 
<link></link> 
<description></description> 
<item> 
    <title>TEST - Funeral Administrator</title> 
    <link> https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run//ETREC107GF.open?VACANCY_ID%3d3416494H67&amp;WVID=6405682s15</link> 
    <guid> https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run//ETREC107GF.open?VACANCY_ID%3d3416494H67&amp;WVID=6405682s15</guid> 
    <description><![CDATA[<b>Reference:</b> REQ000072<br /><b>Description:</b> Applications are invited for a part time, 22.5 hours per week, Funeral Administratorwhich will be based at West Bromwich Funeral Home within the West Region. Working as part of a team, duties will include co-ordinating funeral arrangements according to the clients' wishes, compiling the required administration, offering advice on the full range of services available and liaising with all relevant authorities and internal contacts. You will also be expected to assist families who wish to pay their last respects.You will be required to administer petty cash, cheque reconciliation forms, banking and purchase ledger. Other general office duties include answering the telephones, basic computer input and filing will also be key duties of this role.Applicants will have good inter-personal skills, driving licence and must also have good keyboard skills and computer literate. A flexible approach to working hours is also required.<br /><b>Region:</b> Birmingham<br /><b>Location:</b> West Bromwich<br /><b>Salary:</b> from £7.31 per hour<br /><b>Package:</b> n/a ]]></description> 
    <job:description>Applications&#x20;are&#x20;invited&#x20;for&#x20;a&#x20;part&#x20;time&#x2c;&#x20;22.5&#x20;hours&#x20;per&#x20;week&#x2c;&#x20;Funeral&#x20;Administratorwhich&#x20;will&#x20;be&#x20;based&#x20;at&#x20;West&#x20;Bromwich&#x20;Funeral&#x20;Home&#x20;within&#x20;the&#x20;West&#x20;Region.&#x20;Working&#x20;as&#x20;part&#x20;of&#x20;a&#x20;team&#x2c;&#x20;duties&#x20;will&#x20;include&#x20;co-ordinating&#x20;funeral&#x20;arrangements&#x20;according&#x20;to&#x20;the&#x20;clients&#x27;&#x20;wishes&#x2c;&#x20;compiling&#x20;the&#x20;required&#x20;administration&#x2c;&#x20;offering&#x20;advice&#x20;on&#x20;the&#x20;full&#x20;range&#x20;of&#x20;services&#x20;available&#x20;and&#x20;liaising&#x20;with&#x20;all&#x20;relevant&#x20;authorities&#x20;and&#x20;internal&#x20;contacts.&#x20;You&#x20;will&#x20;also&#x20;be&#x20;expected&#x20;to&#x20;assist&#x20;families&#x20;who&#x20;wish&#x20;to&#x20;pay&#x20;their&#x20;last&#x20;respects.You&#x20;will&#x20;be&#x20;required&#x20;to&#x20;administer&#x20;petty&#x20;cash&#x2c;&#x20;cheque&#x20;reconciliation&#x20;forms&#x2c;&#x20;banking&#x20;and&#x20;purchase&#x20;ledger.&#x20;Other&#x20;general&#x20;office&#x20;duties&#x20;include&#x20;answering&#x20;the&#x20;telephones&#x2c;&#x20;basic&#x20;computer&#x20;input&#x20;and&#x20;filing&#x20;will&#x20;also&#x20;be&#x20;key&#x20;duties&#x20;of&#x20;this&#x20;role.Applicants&#x20;will&#x20;have&#x20;good&#x20;inter-personal&#x20;skills&#x2c;&#x20;driving&#x20;licence&#x20;and&#x20;must&#x20;also&#x20;have&#x20;good&#x20;keyboard&#x20;skills&#x20;and&#x20;computer&#x20;literate.&#x20;A&#x20;flexible&#x20;approach&#x20;to&#x20;working&#x20;hours&#x20;is&#x20;also&#x20;required.</job:description> 
    <job:reference>REQ000072</job:reference> 
    <job:salary>from&#x20;&#xa3;7.31&#x20;per&#x20;hour</job:salary> 
    <job:salaryval>14800</job:salaryval> 
    <job:minsalary>14000</job:minsalary> 
    <job:maxsalary>16000</job:maxsalary> 
    <job:category>Funeral</job:category> 
    <about>Mon, 8 Aug 2011 00:00:00 GMT</about> 
    <job:region>Birmingham</job:region> 
    <job:location>West&#x20;Bromwich</job:location> 
    <job:package>n&#x2f;a</job:package> 
    <pubDate>Mon, 25 Jul 2011 00:00:00 GMT</pubDate> 
</item> 
... etc - other <item> nodes follow 

私は

$category = $_POST['category']; 
$region = $_POST['region']; 

から2 PHP変数を取得し、私はこれらの変数に基づいて、私のXMLをフィルタリングすることができるようにしたいが、実行する方法を見つけ出すことはできませんそれ。私のXMLの領域ノードとカテゴリノードの前には名前空間があることに注意してください。

は、ここに私(そう笑っていない非常に基本的な)PHP

<?php 
$xml = simplexml_load_file("jobs-rss.xml"); 
if(isset($_POST['category']) && isset($_POST['region'])) 
{ 
$category = $_POST['category']; 
$region = $_POST['region']; 
$html = ''; 

if($category == "All" && $region == "All") 
{   
    // Load all jobs 
    foreach($xml->channel->item as $item){ 
    $ns = $item->children('https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run/../../webrecRSS.dtd'); 

    $html .= "<h2>" . $item->title . "</h2>"; 
    $html .= "<p><strong>Reference: </strong>" . $ns->reference . "</p>"; 
    $html .= "<p>" . $ns->description . "</p>"; 
    $html .= "<p><strong>Salary: </strong>" . $ns->salary . "</p>"; 
    $html .= "<p><strong>Region: </strong>" . $ns->region . "</p>"; 
    $html .= "<p><strong>Location: </strong>" . $ns->location . "</p>"; 
    $html .= "<p><strong>Category: </strong>" . $ns->category . "</p>"; 
    $html .= "<p><strong>Closing Date: </strong>" . $ns->closingdate . "</p>"; 
    $html .= "<p><a href='" . $item->link . "'>Click here to apply for this job</a></p>"; 
    } 
} 
else 
{ 
    // Filter my XML here based on a combination of the $category and $region varaibles 
}  
} 

echo $html; 

?> 

だ任意の助けいただければ幸いです。

おかげで、 ブレット

+0

「フィルタ」とはどういう意味ですか? –

答えて

1

フィルタリングすることで、あなたはこのことを意味しましたか?ところで

<?php 

$xml = simplexml_load_file("jobs-rss.xml"); 

if(isset($_POST['category']) && isset($_POST['region'])) 
{ 
    $category = $_POST['category']; 
    $region = $_POST['region']; 
    $html = ''; 

    // Load all jobs 
    foreach($xml->channel->item as $item) 
    { 
     $ns = $item->children('https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run/../../webrecRSS.dtd'); 

     if ($category != "All" && $category != $ns->category) continue; 
     if ($region != "All" && $region != $ns->region) continue; 

     $html .= "<h2>" . $item->title . "</h2>"; 
     $html .= "<p><strong>Reference: </strong>" . $ns->reference . "</p>"; 
     $html .= "<p>" . $ns->description . "</p>"; 
     $html .= "<p><strong>Salary: </strong>" . $ns->salary . "</p>"; 
     $html .= "<p><strong>Region: </strong>" . $ns->region . "</p>"; 
     $html .= "<p><strong>Location: </strong>" . $ns->location . "</p>"; 
     $html .= "<p><strong>Category: </strong>" . $ns->category . "</p>"; 
     $html .= "<p><strong>Closing Date: </strong>" . $ns->closingdate . "</p>"; 
     $html .= "<p><a href='" . $item->link . "'>Click here to apply for this job</a></p>"; 
    }  
} 

echo $html; 

?> 

、あなたの名前空間URL "https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run/../../webrecRSS.dtdは" 少し奇妙に見えます。 https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/webrecRSS.dtd SimpleXMLのchild()メソッドは、 RSSノードのxmlns:job属性で提供されているものとまったく同じURIでのみ動作します。

+0

Imi、$ category変数と$ region変数は、フォームからPOSTによって取り込まれます。 XMlをフィルタリングし、一致するアイテムのみを返したいとします。カテゴリが葬儀の場合は、を返します。ここで、<職種:カテゴリ>は葬儀です。 – Brett

関連する問題