在 PHP 中使用 XPath 过滤 XML 数据(带有命名空间)
希望您能帮助 PHP 新手(向我展示 C#,我就很好!)。
我想基于 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&WVID=6405682s15</link>
<guid> https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run//ETREC107GF.open?VACANCY_ID%3d3416494H67&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 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.</job:description>
<job:reference>REQ000072</job:reference>
<job:salary>from £7.31 per 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 Bromwich</job:location>
<job:package>n/a</job:package>
<pubDate>Mon, 25 Jul 2011 00:00:00 GMT</pubDate>
</item>
... etc - other <item> nodes follow
我从中获取了 2 个 PHP 变量
$category = $_POST['category'];
$region = $_POST['region'];
我希望能够根据这些变量过滤我的 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;
?>
任何帮助将不胜感激。
谢谢, 布雷特
hoping you can help out a PHP newbie (show me C# and I'm fine!).
I want to filter XML data based on a PHP variable. Here's a cut of my XML file:
<?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&WVID=6405682s15</link>
<guid> https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run//ETREC107GF.open?VACANCY_ID%3d3416494H67&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 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.</job:description>
<job:reference>REQ000072</job:reference>
<job:salary>from £7.31 per 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 Bromwich</job:location>
<job:package>n/a</job:package>
<pubDate>Mon, 25 Jul 2011 00:00:00 GMT</pubDate>
</item>
... etc - other <item> nodes follow
I get 2 PHP variables from
$category = $_POST['category'];
$region = $_POST['region'];
I want to be able to filter my XML based on these variables but can't figure out how to do it. Note that the region and category nodes in my XML are preceded with a namespace.
Here's my (very basic so don't laugh) 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;
?>
Any help would be greatly appreciated.
Thanks,
Brett
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
过滤,你的意思是这个吗?
顺便说一句,您的命名空间网址“ 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 属性。
By filtering, did you mean this?
By the way, your namespace url " https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/wrd/run/../../webrecRSS.dtd " looks a bit strange. I think it would be more clear if you remove the relative '..' stuff and make it like this: https://itrent-web1.midlandsco-op.com/tlive_webrecruitment/webrecRSS.dtd
SimpleXML's child() method will only work with exactly the same URI that is provided in the
RSS node's xmlns:job attribute.