Hello,
I am new to xml, xsl, but I want to use node_xslt to Transform XML documents.
I have seen the nodejs page for this https://www.npmjs.com/package/node_xslt and https://developer.mozilla.org/en-US/docs/XSLT/PI_Parameters, but it does not contains any example, so can someone provide me some link or examples through which I can understand about this..
Thank you very much , I have got answer of my question,
The Link http://www.tutorialspoint.com/xslt/xslt_syntax.htm is very useful,
I have require to do just the following
var xslt = require('node_xslt');
var document = xslt.readXmlFile('path of xml file');
var stylesheet = xslt.readXsltFile('path of xslt file');
var transformedString = xslt.transform(stylesheet, document, []);
This will give us the HTML by using xml and xslt, can be check by print this..
console.log("transformedString>>>>" + transformedString);
Related
I want to preface this by saying that I understand there are a lot of similar questions out there (on stack and other sites) related to this problem, but I have spent the past few hours scouring every nook and cranny of the internet and am still coming up dry.
For reference, I've examined the following sites which have provided some guidance, but generally left me more confused than I was before:
https://humanwhocodes.com/blog/2009/03/24/xpath-in-javascript-part-2/
Javascript Xpath and default namespaces
Give me an example of performing an xpath query on a KML document, from Javascript
XPath and Namespaces
Alright, onto the question. I am using Javascript and XPath to try and read a KML file. I use FileReader to get the contents of the XML file, then use the following code to try and access the XML nodes inside.
const xmlParser = new DOMParser();
const xml = xmlParser.parseFromString(contents,"text/xml"); // Contents variable contains the contents of the file (as read by FileReader)
// Read XML
if (xml.evaluate) {
// Most major browsers (except IE)
var path = "/kml/Document/Folder[1]/Placemark/Point/coordinates";
var evaluator = new XPathEvaluator();
var resolver = evaluator.createNSResolver(xml.documentElement);
var nodes = xml.evaluate(path, xml.documentElement, resolver, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
while (result) {
console.log(result);
result = nodes.iterateNext();
}
}
My KML file begins with:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>-/XDF</name>
From this I understand that the xmlns property defines the namespace. My Javascript code works as expected when I remove this property from the node (as the file no longer uses a namespace), which leads me to believe that the issue is that I'm not defining a correct namespace for XPath to use.
The resolver variable is something that I copied from one of the sites linked above. I have tried to find a proper reference to how to generate a namespace resolver but cannot find something which works with my KML file.
Is anyone with more experience in this area able to point me in the right direction to generating a namespace object which will allow me to read the KML file?
Thanks to #dandavis for the prompt.
I needed to create a separate function to return the fixed namespace:
function resolver() {
return 'http://www.opengis.net/kml/2.2';
}
and reference my XPath path with the myns: prefix to each node.
var path = "//myns:Document/myns:Folder[1]/myns:Placemark/myns:Point/myns:coordinates";
Below is the amended routine (which references the resolver function above) in case it helps anyone else:
const xmlParser = new DOMParser();
const xml = xmlParser.parseFromString(contents,"text/xml");
// Read XML
if (xml.evaluate) {
// Most major browsers (except IE)
var path = "//myns:Document/myns:Folder[1]/myns:Placemark/myns:Point/myns:coordinates";
var nodes = xml.evaluate(path, xml.documentElement, resolver, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
while (result) {
console.log(result);
result = nodes.iterateNext();
}
}
With XPath 2 and later as supported by Saxon-JS 2 (https://www.saxonica.com/saxon-js/documentation/index.html#!api/xpathEvaluate) you can keep your XPath paths to elements compact and prefix-less by declaring a default namespaces for the XPath evaluation
var path = '//Document/Folder[1]/Placemark/Point/coordinates';
var result = SaxonJS.XPath.evaluate(path, xml, { xpathDefaultNamespace : 'http://www.opengis.net/kml/2.2' });
console.log(result);
I want to write some data in an existing local XML file with Javascript with some text from an Html page. Is it possible to change content of nodes?
Here is XML sample:
<Notepad>
<Name>Player1</Name>
<Notes>text1</Notes>
</Notepad>
I will get some more text from input and want to add it after "text1", but can't find a solution.
function SaveNotes(content,player)
{
var xml = "serialize.xml";
var xmlTree = parseXml("<Notepad></Notepad>");
var str = xmlTree.createElement("Notes");
$(xmlTree).find("Notepad").find(player).append(str);
$(xmlTree).find("Notes").find(player).append(content);
var xmlString = (new XMLSerializer()).serializeToString(xmlTree);
}
Here is the code to manipulate xml content or xml file :
[Update]
Please check this Fiddle
var parseXml;
parseXml = function(xmlStr) {
return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
};
var xmlTree = parseXml("<root></root>");
function add_children(child_name, parent_name) {
str = xmlTree.createElement(child_name);
//strXML = parseXml(str);
$(xmlTree).find(parent_name).append(str);
$(xmlTree).find(child_name).append("hello");
var xmlString = (new XMLSerializer()).serializeToString(xmlTree);
alert(xmlString);
}
add_children("apple", "root");
add_children("orange", "root");
add_children("lychee", "root");
you can use it for searching in xml as well as adding new nodes with content in it. (And sorry i dont know how to load xml from client side and display it.)
but this fiddle demo will be helpful in adding content in xml and searching in it.
Hope it helps :)
If you want to achieve this on the client side you can parse your xml into a document object:
See
https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML
and
http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2
And then manipulate it like you would the DOM of any html doc, e.g. createElement, appendChild etc.
See https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
Then to serialize it into a String again you could use https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML
Persisting the data
Writing to a local file is not possible in a cross-browser way. In IE you could use ActiveX to read/write file.
You could use cookies to store data on the client side, if your data keeps small enough.
In HTML5 you could use local storage, see http://www.w3schools.com/html/html5_webstorage.asp
Try to use these two package one to convert to json and when is finish the other to come back
https://www.npmjs.com/package/xml2json
https://www.npmjs.com/package/js2xmlparser
I´m trying to get the elements from a web page in Google spreadsheet using:
function pegarAsCoisas() {
var html = UrlFetchApp.fetch("http://www.saosilvestre.com.br").getContentText();
var elements = XmlService.parse(html);
}
However I keep geting the error:
Error on line 2: Attribute name "itemscope" associated with an element type "html" must be followed by the ' = ' character. (line 4, file "")
How do I solve this? I want to get the H1 text from this site, but for other sites I´ll have to select other elements.
I know the method XmlService.parse(html) works for other sites, like Wikipedia. As you can see here.
The html isn't xml. And you don't need to try to parse it. You need to use string methods:
function pegarAsCoisas() {
var urlFetchReturn = UrlFetchApp.fetch("http://www.saosilvestre.com.br");
var html = urlFetchReturn.getContentText();
Logger.log('html.length: ' + html.length);
var index_OfH1 = html.indexOf('<h1');
var endingH1 = html.indexOf('</h1>');
Logger.log('index_OfH1: ' + index_OfH1);
Logger.log('endingH1: ' + endingH1);
var h1Content = html.slice(index_OfH1, endingH1);
var h1Content = h1Content.slice(h1Content.indexOf(">")+1);
Logger.log('h1Content: ' + h1Content);
};
The XMLService service works only with 100% correct XML content. It's not error tolerant. Google apps script used to have a tolerant service called XML service but it was deprecated. However, it still works and you can use that instead as explained here: GAS-XML
Technically HTML and XHTML are not the same. See What are the main differences between XHTML and HTML?
Regarding the OP code, the following works just fine
function pegarAsCoisas() {
var html = UrlFetchApp
.fetch('http://www.saosilvestre.com.br')
.getContentText();
Logger.log(html);
}
As was said on previous answers, other methods should be used instead of using the XmlService directly on the object returned by UrlFetchApp. You could try first to convert the web page source code from HTML to XHTML in order to be able to use the Xml Service Service (XmlService), use the Xml Service as it could work directly with HTML pages, or to handle the web page source code directly as a text file.
Related questions:
How to parse an HTML string in Google Apps Script without using XmlService?
What is the best way to parse html in google apps script
Try replace itemscope by itemscope = '':
function pegarAsCoisas() {
var html = UrlFetchApp.fetch("http://www.saosilvestre.com.br").getContentText();
html = replace("itemscope", "itemscope = ''");
var elements = XmlService.parse(html);
}
For more information, look here.
am doing a project it requires a web site.on this site i have to darw state diagram for hyperlinks.that is how the hyperlinks are attached to one another on a site.am using html.how to get hyperlink id in another html file.i know about document.getElementById.
Thanks inadvance
That would require a way to access another HTML file through AJAX, which is not possible if it isn't on your domain or if CORS isn't enabled.
There's however quite a few things you could do:
Use your own server-side as proxy for fetching the HTML file.
Do the processing on the server-side and let JavaScript plot the data.
Do everything on the server-side.
If you'd like to get the ID's of a link you should use a HTML parser. Modern browsers include a such, it's called DOMParser. You'd do something like this:
var parser = new DOMParser();
var doc = parser.parseFromString(yourHTMLSource, 'text/html');
var links = doc.getElementsByTagName('a');
for(var i = 0, length = links.length; i < length; i++) {
links[i].getAttribute('id'); // -> Returns the ID of the link, if any
}
As I remember it, IE doesn't support this, but has it's own module for HTML parsing with some different methods, but still relatively easy to use.
I'm using Python markdown with Django. It works perfectly. But static HTML previewer it is not enough for an admin panel. I can't find any dynamic JS markdown previewer (I don't need a full converter). Please, advise a javascript markdown previewer.
You're probably looking for showdown.js
Here's an article about it: Using Showdown with and without jQuery
It boils down to this:
var converter = new Showdown.converter();
var input = $("textarea");
var preview = $("#preview");
$(input).keyup(function() {
preview.html(converter.makeHtml(input.val());
});
As another approach, you can use the free API provided in this page:
https://helloacm.com/markdown/
https://helloacm.com/api/markdown/?cached&s=Markdown rocks!
It will return JSON-encoded data:
"<p>Markdown rocks!<\/p>"