Skip to content

Instantly share code, notes, and snippets.

@mhogeweg
Last active February 12, 2016 19:34
Show Gist options
  • Save mhogeweg/5f61058bff2f75affcbb to your computer and use it in GitHub Desktop.
Save mhogeweg/5f61058bff2f75affcbb to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script>
var isIE = /*@cc_on!@*/false || document.documentMode;
var xmlFileName = "iso_dataset.xml";
var xpathTitle = "/gmd:MD_Metadata/gmd:identificationInfo//gmd:citation//gmd:title/gco:CharacterString";
var xpathAbstract = "/gmd:MD_Metadata/gmd:identificationInfo//gmd:abstract/gco:CharacterString";
var xpathPurpose = "/gmd:MD_Metadata/gmd:identificationInfo//gmd:purpose/gco:CharacterString";
var xpathKeywords = "/gmd:MD_Metadata/gmd:identificationInfo//gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/gco:CharacterString";
var xpathLicense = "/gmd:MD_Metadata/gmd:identificationInfo//gmd:resourceConstraints/gmd:MD_LegalConstraints/gmd:useLimitation/gco:CharacterString";
var xpathCredits = "/gmd:MD_Metadata/gmd:contact/gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString";
function isoNSResolver(prefix) {
var ns = {
'gmd' : 'http://www.isotc211.org/2005/gmd',
'gco': 'http://www.isotc211.org/2005/gco',
'srv': 'http://www.isotc211.org/2005/srv'
};
return ns[prefix] || null;
}
function getXMLField(xmlDoc,xpathExpression) {
var theField = "";
//var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement );
var resultType = 0; //XPathResult.ANY_TYPE;
var result;
if (isIE) {
// code for IE
xmlDoc.setProperty("SelectionNamespaces", "xmlns:gmd='http://www.isotc211.org/2005/gmd' xmlns:gco='http://www.isotc211.org/2005/gco' xmlns:srv='http://www.isotc211.org/2005/srv'");
xmlDoc.setProperty("SelectionLanguage", "XPath");
result =xmlDoc.selectNodes(xpathExpression);
} else {
result = xmlDoc.evaluate( xpathExpression, xmlDoc, isoNSResolver, resultType, null );
}
var thisNode;
if (isIE) {
// code for IE
thisNode = result.nextNode();
} else {
thisNode = result.iterateNext();
}
// if there's more than one result node, concatenate with comma.
// always start with a , and then return starting from the second character
while (thisNode) {
if (isIE) {
// code for IE
theField += "," + thisNode.text;
thisNode = result.nextNode();
} else {
theField += "," + thisNode.textContent;
thisNode = result.iterateNext();
}
}
if (theField.length > 0) {
theField = theField.substring(1);
}
return theField;
}
</script>
</head>
<body>
<h1>Extracting Item Info Elements</h1>
<div>
<b>Title:</b> <span id="title"></span><br>
<b>Abstract:</b> <span id="abstract"></span><br>
<b>Purpose:</b> <span id="purpose"></span><br>
<b>Keywords:</b> <span id="keywords"></span><br>
<b>License:</b> <span id="license"></span><br>
<b>Credits:</b> <span id="credits"></span><br>
</div>
<script>
/*
* when reading from a URL
*/
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",xmlFileName,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
/*
* when reading from string
* txt = string containing XML
*/
/*
if (window.DOMParser) {
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
} else {
// Internet Explorer
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}
*/
document.getElementById("title").innerHTML= getXMLField(xmlDoc,xpathTitle);
document.getElementById("abstract").innerHTML= getXMLField(xmlDoc,xpathAbstract);
document.getElementById("purpose").innerHTML= getXMLField(xmlDoc,xpathPurpose);
document.getElementById("keywords").innerHTML= getXMLField(xmlDoc,xpathKeywords);
document.getElementById("license").innerHTML= getXMLField(xmlDoc,xpathLicense);
document.getElementById("credits").innerHTML= getXMLField(xmlDoc,xpathCredits);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment