Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析PubMed查询的curl结果并将其格式化为引用_Php_Xslt_Curl_Xpath_Pubmed - Fatal编程技术网

Php 解析PubMed查询的curl结果并将其格式化为引用

Php 解析PubMed查询的curl结果并将其格式化为引用,php,xslt,curl,xpath,pubmed,Php,Xslt,Curl,Xpath,Pubmed,这是问题的后续问题 同样的想法:我从PubMed中提取数据作为XML,并使用curl来处理这些结果。这允许我获取所需的信息(pub id列表),并将其用作另一个PubMed scrape的变量。$name最终将是动态的 <?php $name = 'white,theodore'; // Return xml data from PubMed based on author search name $ch = curl_init(); curl_setopt($ch, CURLOPT_U

这是问题的后续问题

同样的想法:我从PubMed中提取数据作为XML,并使用curl来处理这些结果。这允许我获取所需的信息(pub id列表),并将其用作另一个PubMed scrape的变量。$name最终将是动态的

<?php 
$name = 'white,theodore';
// Return xml data from PubMed based on author search name
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term='.$name.'[author]&retmode=xml&retmax=50');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

$output = curl_exec($ch);

curl_close($ch);

// Parse the results and concatenate into a string of Publication IDs
$xml=simplexml_load_string($output);
$idList = $xml->IdList;
$ids = "";
foreach($idList->children() as $id) {
    $ids .= $id . ",";
}

// Plug that string of IDs into another PubMed search, this one returning XML data for Publication Summaries
$path = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id='.$ids;

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $path);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_VERBOSE, 0);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch2, CURLOPT_AUTOREFERER, true);
curl_setopt($ch2, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch2, CURLOPT_FRESH_CONNECT, 1);

$data = curl_exec($ch2);

curl_close($ch2);
// Parse those results and print only what is needed for Citation format
$cxml=simplexml_load_string($data);
foreach($cxml->children() as $docsum) {
  foreach($docsum->children() as $item) {
    foreach($item->children() as $details) {
        if ((string) $details['Name'] === 'Author') {echo $details . "., ";}
    }
    if ((string) $item['Name'] === 'FullJournalName') { echo $item . ". "; }
    if ((string) $item['Name'] === 'Title') { echo "<strong>" . $item . "</strong> "; }
    if ((string) $item['Name'] === 'Volume') { echo "Vol." . $item . ", "; }
    if ((string) $item['Name'] === 'Issue') { echo "Issue" . $item . ". "; }
    if ((string) $item['Name'] === 'PubDate') { echo $item . ". "; }
    foreach($item->children() as $details) {
            if ((string) $details['Name'] === 'PubType') {echo $details . ", ";}
        }
  }
  echo "</br></br>";
}

?>
我还尝试了DOMXPath和GetElementsByTagName,但没有成功


基本上,我被卡住了。我尝试过每一种方法的很多种变体,但都出现了很多错误,我感觉自己好像在绕圈子飞行。任何有更多Xpath或XSLT经验的人都有一个想法?

考虑以下纯XSLT解决方案(无需foreach循环),它处理节点重新排序,甚至最终输出,因为XSLT可以将XML内容转换为文本(即PHP字符串),当然甚至是HTML

保留整个CURL调用,但替换嵌套的
foreach
循环。下图:XSLT脚本作为字符串嵌入,需要在.ini文件中启用PHP的
PHP_xsl
扩展才能访问PHP的内置XSLT 1.0处理器(即libxslt):

/。。。相同的卷发呼叫。。。
//解析这些结果,只打印引用格式所需的内容
$cxml=simplexml\u load\u字符串($data);
$xslstr='1
, 
. 
. 
, 
. 
. 
. 
, 
. 
brbr
';
$xsl=simplexml\u load\u字符串($xslstr);
//XSLT转换
$proc=新的XSLTProcessor;
$proc->importStyleSheet($xsl);
$newXML=$proc->transformToXML($cxml);
echo$newXML;
输出


对不起,是的。它实际上是以引文的形式出现的(未在问题中显示-我将添加它)。理想的结果正是,重新排列引用中的XML节点。哇!谢谢你。我当前收到一个
致命错误:未找到类“XSLTProcessor”
错误,我假设这意味着服务器上未启用php_xsl。我接到一个电话给我们的IS人员。希望他们能打开/安装。抱歉,无法编辑。但同时,你能解释一下代码中的一些东西吗?这样我就可以学习了?我真的很感激。我从未想过将XSLT内联。美好的在属性上,在单引号前使用反斜杠。这就是它将它们识别为字符串的开始/结束吗?另外,为什么在每行的值后面都有空的
?嵌入单引号是为了转义用于包装PHP字符串的单引号。如果您从文件解析XSLT,则不需要它们。空的
将在值之间添加一个空格,否则它们将一起刷新。事实上,我刚刚注意到您使用了标点符号句点和逗号,我也用它们更新了脚本。现在我只需要弄清楚,如果字段为空,如何排除额外的标点符号。我试图重新调整“if:test=”语句的用途,但因为我正在寻找selects的值(特别是问题和页面),我想我得到了所有单引号/双引号。请参阅更新,将每个节点包装在
标记中以避免空标点。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD esummary v1 20041029//EN" "https://eutils.ncbi.nlm.nih.gov/eutils/dtd/20041029/esummary-v1.dtd">
<eSummaryResult>
  <DocSum>
    <Id>27431223</Id>
    <Item Name="PubDate" Type="Date">2016 Oct</Item>
    <Item Name="EPubDate" Type="Date">2016 Sep 23</Item>
    <Item Name="Source" Type="String">Antimicrob Agents Chemother</Item>
    <Item Name="AuthorList" Type="List">
      <Item Name="Author" Type="String">Bhattacharya S</Item>
      <Item Name="Author" Type="String">Sobel JD</Item>
      <Item Name="Author" Type="String">White TC</Item>
    </Item>
    <Item Name="LastAuthor" Type="String">White TC</Item>
    <Item Name="Title" Type="String">A Combination Fluorescence Assay Demonstrates Increased Efflux Pump Activity as a Resistance Mechanism in Azole-Resistant Vaginal Candida albicans Isolates.</Item>
    <Item Name="Volume" Type="String">60</Item>
    <Item Name="Issue" Type="String">10</Item>
    <Item Name="Pages" Type="String">5858-66</Item>
    <Item Name="LangList" Type="List">
    <Item Name="Lang" Type="String">English</Item>
    </Item>
    <Item Name="NlmUniqueID" Type="String">0315061</Item>
    <Item Name="ISSN" Type="String">0066-4804</Item>
    <Item Name="ESSN" Type="String">1098-6596</Item>
    <Item Name="PubTypeList" Type="List">
      <Item Name="PubType" Type="String">Journal Article</Item>
    </Item>
    <Item Name="RecordStatus" Type="String">Unknown status</Item>
    <Item Name="PubStatus" Type="String">epublish</Item>
    <Item Name="ArticleIds" Type="List">
      <Item Name="pubmed" Type="String">27431223</Item>
      <Item Name="pii" Type="String">AAC.01252-16</Item>
      <Item Name="doi" Type="String">10.1128/AAC.01252-16</Item>
      <Item Name="pmc" Type="String">PMC5038269</Item>
      <Item Name="rid" Type="String">27431223</Item>
      <Item Name="eid" Type="String">27431223</Item>
      <Item Name="pmcid" Type="String">pmc-id: PMC5038269;embargo-date: 2017/04/01;</Item>
    </Item>
    <Item Name="DOI" Type="String">10.1128/AAC.01252-16</Item>
    <Item Name="History" Type="List">
      <Item Name="received" Type="Date">2016/06/10 00:00</Item>
      <Item Name="accepted" Type="Date">2016/07/12 00:00</Item>
      <Item Name="pmc-release" Type="Date">2017/04/01 00:00</Item>
      <Item Name="entrez" Type="Date">2016/07/20 06:00</Item>
      <Item Name="pubmed" Type="Date">2016/07/20 06:00</Item>
      <Item Name="medline" Type="Date">2016/07/20 06:00</Item>
    </Item>
    <Item Name="References" Type="List"></Item>
    <Item Name="HasAbstract" Type="Integer">1</Item>
    <Item Name="PmcRefCount" Type="Integer">0</Item>
    <Item Name="FullJournalName" Type="String">Antimicrobial agents and chemotherapy</Item>
    <Item Name="ELocationID" Type="String">doi: 10.1128/AAC.01252-16</Item>
    <Item Name="SO" Type="String">2016 Oct;60(10):5858-66</Item>
</DocSum>

</eSummaryResult>
</br></br>
$content = simplexml_load_string($data);
$results = $content->xpath('Item[@Name]');
foreach($results as $result) {
  $title = $result->xpath('[@Name="Title"]');
  // Make sure there's an author attribute
  if($title) {
    // because we have a list of elements even if there's one result
    $items = $title[0]->attributes();
    $title = $items['value'];
  }
echo $results;
}
// ... same CURL call ...

// Parse those results and print only what is needed for Citation format
$cxml=simplexml_load_string($data);

$xslstr = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="DocSum">
    <xsl:if test="Item[@Name=\'AuthorList\' and .!=\'\']">
        <xsl:for-each select="Item[@Name=\'AuthorList\' and .!=\'\']/*">
            <xsl:value-of select="." />
            <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
            </xsl:if>           
        </xsl:for-each><xsl:text>. </xsl:text>
    </xsl:if>

    <xsl:if test="Item[@Name=\'FullJournalName\' and .!=\'\']">
        <xsl:value-of select="concat(\'&lt;strong&gt;\', Item[@Name=\'FullJournalName\'], 
                                     \'&lt;/strong&gt;\')" />
        <xsl:text>. </xsl:text>
    </xsl:if>
    <xsl:if test="Item[@Name=\'Title\' and .!=\'\']">                        
        <xsl:value-of select="Item[@Name=\'Title\']" /><xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:if test="Item[@Name=\'Volume\' and .!=\'\']">
        <xsl:value-of select="Item[@Name=\'Volume\']" /><xsl:text>. </xsl:text>
    </xsl:if>
    <xsl:if test="Item[@Name=\'Issue\' and .!=\'\']">
        <xsl:value-of select="Item[@Name=\'Issue\']" /><xsl:text>. </xsl:text>
    </xsl:if>
    <xsl:if test="Item[@Name=\'PubDate\' and .!=\'\']">
        <xsl:value-of select="Item[@Name=\'PubDate\']" /><xsl:text>. </xsl:text>
    </xsl:if>

    <xsl:if test="Item[@Name=\'PubTypeList\' and .!=\'\']">
        <xsl:for-each select="Item[@Name=\'PubTypeList\']/*">
            <xsl:value-of select="." />
            <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
            </xsl:if>           
        </xsl:for-each><xsl:text>. </xsl:text>
    </xsl:if>

    <xsl:text>&lt;br&gt;&lt;br&gt;</xsl:text>
</xsl:template>     
</xsl:stylesheet>';

$xsl=simplexml_load_string($xslstr);

// XSLT TRANSFORMATION
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 
$newXML = $proc->transformToXML($cxml);

echo $newXML;