我正在努力完成一项任务。我正在尝试创建一个仅显示Journal元素的函数,其中Author1 / Auth1是作者之一。我确实列出了三个Journal元素,但我只能将每个Journal分成一个新行。 我无法将结果中的每个Journal-child(最好是文本nexto值)与新行(<br />)
分开。
我有以下XML文档:
<Journals>
<Journal>
<Title>Title1</Title>
<AuthorName>Author1</AuthorName>
<AuthorName>Author2</AuthorName>
<AuthorName>Author3</AuthorName>
<JournalName>One</JournalName>
</Journal>
<Journal>
<Title>Title2</Title>
<AuthorName>Author2</AuthorName>
<AuthorName>Author1</AuthorName>
<JournalName>Two</JournalName>
</Journal>
<Journal>
<Title>Title3</Title>
<AuthorName>Author3</AuthorName>
<JournalName>Three</JournalName>
</Journal>
<Journal>
<Title>Title4</Title>
<AuthorName>Author3</AuthorName>
<AuthorName>Author2</AuthorName>
<AuthorName>Author4</AuthorName>
<JournalName>Four</JournalName>
</Journal>
<Journal>
<Title>Title5</Title>
<AuthorName>Author2</AuthorName>
<AuthorName>Auth1</AuthorName>
<JournalName>Five</JournalName>
</Journal>
<Journal>
<Title>Title6</Title>
<AuthorName>Author2</AuthorName>
<AuthorName>Author3</AuthorName>
<JournalName>Six</JournalName>
</Journal>
</Journals>
以下PHP文档:
<?php
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/* FUNCTION FOR ALL ARTICLES PUBLISHED BY Author1 */
function taskOne()
{
$doc = new DOMDocument();
$doc->load("file.xml");
$xml = new DOMXPath($doc);
$query = $xml->evaluate("/Journals/Journal/AuthorName[contains(. , 'Author1') or contains(. , 'Auth1')]/..");
foreach ($query as $temp)
{
echo $temp->nodeValue . "<br />";
}
}
taskOne();
?>
现在的另一个问题:如何使用XPath查询:/Journals/Journal[contains(AuthorName, 'Author1') or contains(AuthorName, 'Auth1')]
仅列出作者是最佳作者的日记,而不是那些他的孩子兄弟二号或三号的日记?
以下是XPath查询的最终输出: /Journals/Journal/AuthorName[contains(. , 'Author1') or contains(. , 'Auth1')]/..
Title1 Author1 Author2 Author3 One
Title2 Author2 Author1 Two
Title5 Author2 Auth1 Five
以下是XPath查询的最终输出: /Journals/Journal[contains(AuthorName, 'Author1') or contains(AuthorName, 'Auth1')]
Title1 Author1 Author2 Author3 One
对于澄清,这是我想要实现的目标:
Title: Title1
Journal author: Author1
Journal author: Author2
Journal author: Author3
Journal name: One
Title: Title2
Journal author: Author2
Journal author: Author1
Journal name: Two
Title: Title5
Journal author: Author2
Journal author: Auth1
Journal name: Five