我对PHP和XML还很陌生,遇到了一个关于XML提要的特殊问题。在XML数据中,每个新闻报道都有一个字段"article_content",该字段具有唯一的属性(id)。我需要能够在一个基于索引页面创建的URL的页面上显示这个故事,该页面显示所有故事(URL的示例是path/to/file/newstory.php?storyid=19837775),其中storyid与文章内容字段中的id属性相匹配。
有人能帮我吗,我的头都快撞到墙上了!
更新:
XML的格式如下(每个故事的新文章内容)
<channel>
<article_content id="19837775" status="A">
<title>title of article 1</title>
<date>20120127</date>
<time>10:18:00</time>
<body>main body of story 1 here</body>
<introduction>intro text here</introduction>
<abstract></abstract>
<by_line></by_line>
<category_id>0103</category_id>
</article_content>
[...]
我得到的php代码是:
<?php
$xml = new SimpleXMLElement($rss);
$results = array ();
foreach ($xml->channel->article_content[id] as $item) {
echo "<h3>".$item->title."</h3>";
echo nl2br ($item->body->asXML());
}
?>
如果没有XML,我只能显示以下示例(取自此处):
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;
输出类型属性:
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->rating[0]['type']
此处的示例
首先是一个如何的例子(使用XPath和php的DOM扩展
<?php
$id = '19837775'; // this is the parameter you'd fetch from the url
$doc = new DOMDocument;
//$doc->load('feed.xml');
$doc->loadxml( getData() );
$xpath = new DOMXPath($doc);
foreach ( $xpath->query('story[@article_content="'.$id.'"]') as $story ) {
echo $doc->savexml($story);
}
function getData() {
return <<< eox
<stories>
<story article_content="1">content 1</story>
<story article_content="2">content 2</story>
<story article_content="19837775">content 19837775</story>
<story article_content="22222222">content 22222222</story>
</stories>
eox;
}
打印
<story article_content="19837775">content 19837775</story>
但这可能不是一个好的解决方案。这取决于您实际想要实现的目标(以及解决方案的可扩展性)
请详细说明。
update:使用实际数据结构的示例。
<?php
$id = '19837775'; // the parameter fetched from the request
$doc = new DOMDocument;
//$doc->load('feed.xml');
$doc->loadxml( getData() );
$xpath = new DOMXPath($doc);
$article = firstNode( $xpath->query('article_content[@id="'.$id.'"]') );
if ( !$article ) {
die('no such article');
}
$title = firstNode( $xpath->query('title', $article) );
$date = firstNode( $xpath->query('date', $article) );
$body = firstNode( $xpath->query('body', $article) );
// TODO: check $title, $data and $body
echo 'title: ', $title->nodeValue, "n";
echo 'date: ', $title->nodeValue, "n";
echo 'body: ', $doc->savexml($body), "n";
echo 'body->nodeValue: ', $body->nodeValue;
die;
function firstNode($nodelist) {
if ( $nodelist instanceof DOMNodeList && 0<$nodelist->length ) {
return $nodelist->item(0);
}
return false;
}
function getData() {
return <<< eox
<channel>
<article_content id="1" status="X"></article_content>
<article_content id="19837775" status="A">
<title>title of article 1</title><date>20120127</date><time>10:18:00</time>
<body><h1>main body of story 1</h1><p>Not just plain text</p></body>
<introduction>intro text here</introduction><abstract></abstract><by_line></by_line><category_id>0103</category_id>
</article_content>
<article_content id="20000000" status="X"></article_content>
<article_content id="20000001" status="X"></article_content>
</channel>
eox;
}
打印
title: title of article 1
date: title of article 1
body: <body><h1>main body of story 1</h1><p>Not just plain text</p></body>
body->nodeValue: main body of story 1Not just plain text
你可能也对https://en.wikipedia.org/wiki/XSLTXSL它也使用XPath来选择节点,但让我们将输出进行转换(例如,从xml表示转换为用户友好的html格式)。