gpt4 book ai didi

PHP、XML 和 XPath : Display specific nodes

转载 作者:行者123 更新时间:2023-12-03 16:17:42 25 4
gpt4 key购买 nike

我正在努力完成一项任务。我正在尝试创建一个仅显示 Journal 元素的函数,其中 Author1/Auth1 是作者之一。我确实列出了三个 Journal 元素,但我只能将每个 Journal 分隔成一个新行。 我无法将结果中的每个 Journal-child(最好在值旁边加上文本)分隔到新行 (<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')]只列出 Author1 是第一作者的期刊,而不列出他是二号或三号 sibling 的期刊?这是 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

最佳答案

您可以只针对 <Journal>哪个有 child <AuthorName>包含您想要的那根针。你已经很接近了:

$doc = new DOMDocument();
$doc->loadXML($xml_string);
$xpath = new DOMXpath($doc);

$query = "//Journal[AuthorName[contains(., 'Author1') or contains(., 'Auth1')]]";

$journals = $xpath->query($query);
if($journals->length > 0) {
foreach($journals as $journal) { // for each of the journal found
foreach($journal->childNodes as $e) { // loop all its nodes/children
if(isset($e->tagName)) {
echo "$e->tagName: " . $e->nodeValue . '<br/>';
}
}
echo '<br/>';
}
}

Sample Output

关于PHP、XML 和 XPath : Display specific nodes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26835003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com