Reputation: 3
I'm trying to use Google Apps Script contained in a sheet to return XML from the Wikidata web service for SPARQL queries. I have a simple working SPARQL query and am using the URL-escaped https string, per the instructions in the Wikidata user guide.
Per the code below, I've tried UrlFetchApp.Fetch() both with and without the options parameter, but I always get an empty response. When I test the "simpleQry" url in my browser, it returns the correct xml file. I don't use web services very often, so I may be missing something obvious. Any help is greatly appreciated!
First I set the fetch options with JSON
//set http header values w/json
var headr = {
'Accept': 'application/sparql-results+xml'
};
//set options values w/json
var options = {
'method': 'get',
'header': headr
};
Then I submit the request:
simpleQry = "https://query.wikidata.org/sparql?query=select%20%3FpersonLabel%20%3FemployerLabel%20%3FworksLabel%0Awhere%20%7B%0A%20%20VALUES%20%3Fperson%20%7Bwd%3AQ112129152%7D%20.%0A%20%20%3Fperson%20wdt%3AP108%20%3Femployer%20.%0A%20%20%3Fperson%20wdt%3AP800%20%3Fworks%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20'%5BAUTO_LANGUAGE%5D%2Cen'.%20%7D%0A%20%20%7D";
var response = UrlFetchApp.fetch(simpleQry, options);
var dataAllxml = XmlService.parse(response.getContentText());
Process the results. This where the error gets uncovered. Length of content = 0.
var rows = [], data;
for (i = 0; i < dataAllxml.length; i++) {
data = dataAllxml[i];
rows.push([data.personLabel, data.employerLabel, data.worksLabel]); //
}
Upvotes: 0
Views: 102
Reputation: 201388
XmlService.parse(response.getContentText())
returns XML Object. By this, dataAllxml.length
becomes undefined
. By this, the for loop is not run. I think that this is the reason for your issue of Process the results. This where the error gets uncovered. Length of content = 0.
.When this is reflected in your script, it becomes as follows.
function myFunction() {
var headr = {
'Accept': 'application/sparql-results+xml'
};
var options = {
'method': 'get',
'header': headr
};
var simpleQry = "https://query.wikidata.org/sparql?query=select%20%3FpersonLabel%20%3FemployerLabel%20%3FworksLabel%0Awhere%20%7B%0A%20%20VALUES%20%3Fperson%20%7Bwd%3AQ112129152%7D%20.%0A%20%20%3Fperson%20wdt%3AP108%20%3Femployer%20.%0A%20%20%3Fperson%20wdt%3AP800%20%3Fworks%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20'%5BAUTO_LANGUAGE%5D%2Cen'.%20%7D%0A%20%20%7D";
var response = UrlFetchApp.fetch(simpleQry, options);
var dataAllxml = XmlService.parse(response.getContentText());
// I modified below script.
var root = dataAllxml.getRootElement();
var ns = root.getNamespace();
var rows = root.getChild("results", ns).getChildren().map(e => e.getChildren().map(f => f.getValue().trim()));
console.log(rows);
}
When this modified script is run, the following result is obtained.
[
["Ramona L. Pérez","San Diego State University","Good and bad death: exploring the perspectives of older Mexican Americans."],
["Ramona L. Pérez","San Diego State University","Diabetes Cultural Beliefs and Traditional Medicine Use Among Health Center Patients in Oaxaca, Mexico."],
["Ramona L. Pérez","San Diego State University","Exposure to smoking in soap operas and movies: smoking cessation and attempts to quit."]
]
Upvotes: 0