2016-12-15 13 views
1

これはthis質問へのフォローアップです。PubMedクエリからのカール結果の解析と引用形式への書式設定

私はXMLとしてPubMedからデータを引き出し、curlを使ってその結果を処理しています。これにより、必要な情報(パブIDのリスト)を取得し、それを別のPubMedスクレープの変数として使用することができます。 $ 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>"; 
} 

?> 

これは、次のXMLデータを返します(これは1つの結果です)。

ので、エコー
<?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> 

2016 Oct. Bhattacharya S., Sobel JD., White TC., A Combination Fluorescence Assay Demonstrates Increased Efflux Pump Activity as a Resistance Mechanism in Azole-Resistant Vaginal Candida albicans Isolates. Vol.60, Issue10. Journal Article, Antimicrobial agents and chemotherapy.

  1. Zavrel M., White TC., Medically important fungi respond to azole drugs: an update. Vol.10, Issue8. Journal Article, Review, Future microbiology.

  2. Esquivel BD., Smith AR., Zavrel M., White TC., Azole drug import into the pathogenic fungus Aspergillus fumigatus. Vol.59, Issue6. Journal Article, Antimicrobial agents and chemotherapy.

2015 Apr. Achterman RR., Moyes DL., Thavaraj S., Smith AR., Blair KM., White TC., Naglik JR., Dermatophytes activate skin keratinocytes via mitogen-activated protein kinase signaling and induce immune responses. Vol.83, Issue4. Journal Article, Infection and immunity.

2015 Feb 3. Ford CB., Funt JM., Abbey D., Issi L., Guiducci C., Martinez DA., Delorey T., Li BY., White TC., Cuomo C., Rao RP., Berman J., Thompson DA., Regev A., The evolution of drug resistance in clinical isolates of Candida albicans. Vol.4, Issue. Journal Article, eLife.

2014 Aug 1. White TC., Findley K., Dawson TL Jr., Scheynius A., Boekhout T., Cuomo CA., Xu J., Saunders CW., Fungi on the skin: dermatophytes and Malassezia. Vol.4, Issue8. Journal Article, Review, Cold Spring Harbor perspectives in medicine.

2014 Jan. Maguire SL., Wang C., Holland LM., Brunel F., Neuvéglise C., Nicaud JM., Zavrel M., White TC., Wolfe KH., Butler G., Zinc finger transcription factors displaced SREBP proteins as the major Sterol regulators during Saccharomycotina evolution. Vol.10, Issue1. Journal Article, PLoS genetics.

2013 Nov 15. Campoli P., Perlin DS., Kristof AS., White TC., Filler SG., Sheppard DC., Pharmacokinetics of posaconazole within epithelial cells and fungi: insights into potential mechanisms of action during treatment and prophylaxis. Vol.208, Issue10. Journal Article, The Journal of infectious diseases.

2013 Jul 8. Achterman RR., White TC., Dermatophytes. Vol.23, Issue13. Journal Article, Current biology : CB.

ETC.

すべてが素晴らしい作品と私​​は必要なデータだけを引用を生成するが著者が最初に来るので、私は結果を並べ替えることができない、PUBDATE等、最後に来て私が持っていますいくつかのオプションを試してみましたが、私はそれらのどれかに慣れていないので、それを解読できないようです。

私はXSLTスタイルシートを添付しようとしましたが、XMLファイルを実際に出力していないため動作していないと思います。多分?

引用を返すのに空白の結果が得られるように、下部のSimpleXMLブロックの代わりに次のXPathを使用しようとしました。すべてのデータは<Item>とタグ付けされているため、名前属性を使用するのに失敗しています。

$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; 
} 

DOMXPathとGetElementsByTagNameも無駄にしました。

基本的に、私は立ち往生しています。私はそれぞれの非常に多くのバリエーションを試して、私がサークルで飛んでいるように感じる非常に多くのエラーを得ました。 XpathやXSLTの経験が豊富な人は誰でも考えられますか?

+0

申し訳ありません。それは引用形式で実際にエコーします(問題には示されていません - 私はそれを追加します)。その結果として、その引用内のXMLノードを並べ替えることができます。 – cebronix

答えて

0

XSLTがXMLコンテンツをテキスト(つまりPHP文字列)、さらにはHTMLに変換できるように、ノードの並べ替えや最終出力を処理する、次の純粋なXSLTソリューション(foreachループが必要)を考えてみましょう。

CURL呼び出し全体をそのままにして、ネストされたforeachルーピングを置き換えます。以下は、XSLTスクリプトが(すなわち、libxsltは)文字列として埋め込むとPHPの組み込みのXSLT 1.0プロセッサにアクセスするには、.iniファイルで有効PHPのphp_xsl拡張モジュールを必要とされています

// ... 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; 

出力

Citations Output

+0

うわー!ありがとう。私は現在、 '致命的なエラーが発生しています:クラス 'XSLTProcessor'が見つかりませんでした.'というエラーです。これは、php_xslがサーバ上で有効になっていないことを意味しています。私は私たちの人に電話があります。うまくいけば、彼らはそれをスイッチオン/インストールすることができます。 – cebronix

+0

申し訳ありません、編集できません。しかし、その間に、私は学ぶことができるので、コード内のいくつかのことだけを説明できますか?私は本当にそれを感謝します。 XSLTをインラインに含めることは決して考えませんでした。ニース。属性では、単一引用符の前にバックスラッシュを使用します。それは文字列の開始/終了として認識されますか?また、なぜ各値の後に空の ''? – cebronix

+0

PHP文字列をラップするために使用される単一引用符をエスケープするために、単一引用符が埋め込まれています。ファイルからXSLTを解析する場合、これらは必要ありません。そして、空の ''は値の間にスペースを追加することです。それは一緒にフラッシュされます。実際には、私はちょうど私もあなたのスクリプトを更新している句読点とカンマを使用して気づいた。 – Parfait

関連する問題