转换 Europe PMC RESTful "search" 对 source/external-id 列表的响应
Transforms Europe PMC RESTful "search" responses to source/external-id lists
我想使用 Europe PMC RESTful API 将 "search" 响应转换为 source/external-id 列表,每条记录文本一行。
有任何建议/示例(在 JavaScript 中)吗?
您可以使用 XSLT 转换从欧洲 PMC RESTful API 获得的响应。
您可以找到 an example written in Vue.js on JSBin,您可以在其中找到示例 XSL 文件,以及如何将 XML 响应转换为 JavaScript 中的 ID 列表,如下所示:
示例 XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
Script: rest2ids.xsl
Version: 1.0
Summary: Transforms Europe PMC RESTful "search" responses to source/external-id lists, with one-line per record text.
Notes: Either resulttype=lite or resulttype=core paramater can used
-->
<xsl:output method="text" encoding="UTF-8" />
<xsl:param name="idMode" select="1" />
<!-- 1 = Format IDs as on the front end, 2 = Format IDs for using Excel to generate External Links data -->
<xsl:variable name="newline" select="'
'" />
<xsl:template match="/">
<xsl:for-each select="/responseWrapper/resultList/result">
<xsl:choose>
<xsl:when test="$idMode=1 and source/text()='MED'">
<xsl:text>PMID</xsl:text>
</xsl:when>
<xsl:when test="$idMode=1 and (source/text()='PMC')">
<xsl:text>PMCID</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="source/text()" />
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$idMode=1">
<xsl:text>:</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="id/text()" />
<!-- Output PMCID -->
<xsl:choose>
<xsl:when test="$idMode=1 and source/text()='MED'">
<xsl:if test="pmcid != ''">
<xsl:text>,PMCID:</xsl:text>
<xsl:value-of select="pmcid/text()" />
</xsl:if>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
<!-- End of outputting PMCID -->
<xsl:value-of select="$newline" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<title>JS Bin</title>
</head>
<body>
<div id="app">
<input type="text" size="70" v-model="url">
<fieldset>
<legend>Output</legend>
<textarea id="output" cols="56" rows="10" v-model="output"></textarea>
</fieldset>
<button @click="downloadData">Download</button>
</div>
</body>
</html>
Vue.js
var app = new Vue({
el: '#app',
data() {
return {
url: "https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=cancer",
xhttp: {},
output: ''
};
},
watch: {
url: function() {
this.output = this.transform();
}
},
created() {
if (window.ActiveXObject) {
this.xhttp = new window.ActiveXObject("Msxml2.XMLHTTP");
} else {
this.xhttp = new XMLHttpRequest();
}
},
methods: {
loadXMLDoc: function(filename) {
this.xhttp.open("GET", filename, false);
try {
this.xhttp.responseType = "msxml-document";
} catch (err) {}
this.xhttp.send("");
return this.xhttp.responseXML;
},
download: function(filename, text) {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
downloadData: function() {
this.download('idlist.txt', this.output);
},
transform: function() {
let xml = this.loadXMLDoc(this.url);
let xsl = this.loadXMLDoc("https://yucigou.github.io/staticfs/xsl/rest2ids.xsl");
if (window.ActiveXObject || this.xhttp.responseType === "msxml-document") {
return xml.transformNode(xsl);
} else if (document.implementation && document.implementation.createDocument) {
// code for Chrome, Firefox, Opera, etc.
let xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
let resultDocument = xsltProcessor.transformToFragment(xml, document);
return resultDocument.textContent;
}
}
},
mounted () {
this.output = this.transform();
}
});
当你在浏览器中运行这个例子时,你会看到输出:
您可以更改查询以查看正在更新的输出,也可以将其下载到文件中,如上所示。
我想使用 Europe PMC RESTful API 将 "search" 响应转换为 source/external-id 列表,每条记录文本一行。
有任何建议/示例(在 JavaScript 中)吗?
您可以使用 XSLT 转换从欧洲 PMC RESTful API 获得的响应。
您可以找到 an example written in Vue.js on JSBin,您可以在其中找到示例 XSL 文件,以及如何将 XML 响应转换为 JavaScript 中的 ID 列表,如下所示:
示例 XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
Script: rest2ids.xsl
Version: 1.0
Summary: Transforms Europe PMC RESTful "search" responses to source/external-id lists, with one-line per record text.
Notes: Either resulttype=lite or resulttype=core paramater can used
-->
<xsl:output method="text" encoding="UTF-8" />
<xsl:param name="idMode" select="1" />
<!-- 1 = Format IDs as on the front end, 2 = Format IDs for using Excel to generate External Links data -->
<xsl:variable name="newline" select="'
'" />
<xsl:template match="/">
<xsl:for-each select="/responseWrapper/resultList/result">
<xsl:choose>
<xsl:when test="$idMode=1 and source/text()='MED'">
<xsl:text>PMID</xsl:text>
</xsl:when>
<xsl:when test="$idMode=1 and (source/text()='PMC')">
<xsl:text>PMCID</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="source/text()" />
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$idMode=1">
<xsl:text>:</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="id/text()" />
<!-- Output PMCID -->
<xsl:choose>
<xsl:when test="$idMode=1 and source/text()='MED'">
<xsl:if test="pmcid != ''">
<xsl:text>,PMCID:</xsl:text>
<xsl:value-of select="pmcid/text()" />
</xsl:if>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
<!-- End of outputting PMCID -->
<xsl:value-of select="$newline" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<title>JS Bin</title>
</head>
<body>
<div id="app">
<input type="text" size="70" v-model="url">
<fieldset>
<legend>Output</legend>
<textarea id="output" cols="56" rows="10" v-model="output"></textarea>
</fieldset>
<button @click="downloadData">Download</button>
</div>
</body>
</html>
Vue.js
var app = new Vue({
el: '#app',
data() {
return {
url: "https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=cancer",
xhttp: {},
output: ''
};
},
watch: {
url: function() {
this.output = this.transform();
}
},
created() {
if (window.ActiveXObject) {
this.xhttp = new window.ActiveXObject("Msxml2.XMLHTTP");
} else {
this.xhttp = new XMLHttpRequest();
}
},
methods: {
loadXMLDoc: function(filename) {
this.xhttp.open("GET", filename, false);
try {
this.xhttp.responseType = "msxml-document";
} catch (err) {}
this.xhttp.send("");
return this.xhttp.responseXML;
},
download: function(filename, text) {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
downloadData: function() {
this.download('idlist.txt', this.output);
},
transform: function() {
let xml = this.loadXMLDoc(this.url);
let xsl = this.loadXMLDoc("https://yucigou.github.io/staticfs/xsl/rest2ids.xsl");
if (window.ActiveXObject || this.xhttp.responseType === "msxml-document") {
return xml.transformNode(xsl);
} else if (document.implementation && document.implementation.createDocument) {
// code for Chrome, Firefox, Opera, etc.
let xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
let resultDocument = xsltProcessor.transformToFragment(xml, document);
return resultDocument.textContent;
}
}
},
mounted () {
this.output = this.transform();
}
});
当你在浏览器中运行这个例子时,你会看到输出:
您可以更改查询以查看正在更新的输出,也可以将其下载到文件中,如上所示。