Jump to content

Edit Xsl File


bakunin

Recommended Posts

Is it possible to navigate through an xsl file and edit a template in it and save it like if it was an xml files with javascript ?Lets say i want to edit a tag with the id="4",then when its an xml file i would do a:

var path = "//tag[@id[text()='"4"']]";var node = SelectSingleNode(xsl,path);

if my xml file is

<tag = "4">blabla</tag>;

But what if the file i want to edit is an xsl and it is:

	 <xsl:template match="tag">				 description of the tag	</xsl:template>

how do i select that template and then save it back to the server ?

Link to comment
Share on other sites

You can't save the template back to the server with JavaScript. Not unless you want to allow anyone of your visitors (or rather - the hackers among them) to do the same.You can do it on the server itself, with the same APIs that are used for manipulating XML documents. Ideally, with DOM.BTW, I think you mean the XML is:

<tag id="4">blabla</tag>;

What you showed is not a valid XML constuct.

Link to comment
Share on other sites

Thanks for the reply, actually i already know how to edit an xml file with DOM, i was wondering if we could edit an XSL with dom also.As for editing it with Javascript, you are right, i can't edit it. What i was thinking is using JQuery, $.GET(), then send my info to a php file that modify the xsl file. Im gonna need javascript to navigate in the XSL file and populate my textarea with my XSL code to modify and to send the info to the php when i click save.All i need is to find out how to extract the code of a XSL template and how to push it back. I was wondering if DOM could navigate through an XSL file like it does in a XML file.All i can think of else is reading it like a text file and slice it a different way using find/replace or something else, not really pretty code.

Link to comment
Share on other sites

"DOM" and "JavaScript" are two different things. DOM is an API (a set of functions and classes). PHP also has a DOM API. Since DOM is a W3C standard, the DOM in PHP is the same as JavaScript's DOM. Browser vendors have implemented additional features (or have implemented some of them differently), but the part that is a standard is the one where you can see in both PHP and JavaScript.Like I said, you can edit XSLT with this same API. The DOM can be used to edit any XML document, including XSLT. PHP however can also save() it.You'll have to first read out the portion(s) you want to edit with DOM, send them to the client (whether its by a response to an AJAX request or upon a page load is irrelevant). Then let the client send in a new version for that template, check the XML for well-formdness if you must, then read the XSLT file again, and write up this new portion.The exact code for this is not trivial, so... good luck. But still, keep in mind that using PHP's DOM is basically a must. The other alternative is SimpleXML, but that API doesn't exactly play well with namespaced documents such as XSLT.

Link to comment
Share on other sites

k, nice about the code, the specific thing that i struggle to do is to get the tag i want.Like in the first sample, thats what i do when i travel through an xml document with dom.

var path = "//tag[@id[text()='"4"']]";var node = SelectSingleNode(xsl,path);if my xml file is: <tag id="4">blabla</tag>;

An xsl file is an xml document but it has a different structure "xsl:____" like this:

 <xsl:template match="tag">				 description of the tag	</xsl:template>

So my question is how can i adapt my path to obtain a node in xsl:

var path = "????";var node = SelectSingleNode(xsl,path);

i guess its something like:

var path = "xsl:template[@match='tag']";var node = SelectSingleNode(xsl,path);

But im wondering how to deal with the ":" what does it stand for ? should i just ignore it or is it only "xsl" or only "template".

Link to comment
Share on other sites

Those are XML namespaces... one more reason to use PHP instead of JavaScript. Support for namespaces in browsers (especially IE) is poor.In PHP, you can use getElementByTagNameNS() to get only the 'template' elements in the XSLT namespace. You need to specify the XSLT namespace URI btw, not the namespace prefix.In a similar fashion, you can use DOMXPath() to use XPath instead of DOM methods. You can map the XSLT namespace URI to a prefix (like 'xsl') with the DOMXPath::registerNamespace() method, then write the XPath expression as '//xsl:template[@match=tag]' (for example).Look into the link from my previous post for details.

Link to comment
Share on other sites

I prefer by far PHP and would always use it when possible but for this project, i need to use Javascript to populate my textarea because i will be navigating through my record and i don't want my page to refresh evry time i do so.

Link to comment
Share on other sites

I prefer by far PHP and would always use it when possible but for this project, i need to use Javascript to populate my textarea because i will be navigating through my record and i don't want my page to refresh evry time i do so.
You can still use PHP to populate the textarea on load, and then once to update the result.If you really must, you can perform a separate HTTP request with JavaScript's XMLHttpRequest() object. That would let JavaScript ask PHP to fetch the XSLT template, and give it back to JavaScript (in text form). JavaScript can then populate the textarea with that text. Read W3Schools' "AJAX" tutorial or XML HTTP Request part of the XML tutorial if you aren't already aware of XMLHttpRequest().
Link to comment
Share on other sites

Great, im very close now to make it work. Im gonna look more closely about using PHP once i get the path to work.This works:

var path = "xsl:template[@match='tag']";

but most of my templates in xsl look like this:

<xsl:template match="object[@type='PANEL']">...</xsl:template>

So it does not work because i use [ ] within the value of match so my path get confused.This does not work:

var obj = "object[@type='PANEL']	"var path = "xsl:template[@match='"+obj+"']";// would look like this without the "":  xsl:template[@match='object[@type='PANEL']']";

Link to comment
Share on other sites

You need to encode those characters as [ and ], like so:

var obj = "object[@type='PANEL']";

(but add in a ";" after the numbers. I've omitted it, because this board interprets entities)If your JavaScript is within your (X)HTML document, you must even double encode them (once for the (X)HTML parser, a second time for JavaScript):

var obj = "object&#91;@type='PANEL'&#93;";

Link to comment
Share on other sites

I tried that yesterday but without the ; and amp so im gonna try that tonight. The other way around i found was to name my template and use @name='______'. So for the HTTP request thing, that is ajax right ?i know how to send var to a php file and execute it in the background, $.GET $.POST $.ajax. In other word: Javascript to PHPI found it very usefull but with HTTP request i can do:Javascript to PHP to Javascript ?in the sample i do not see a php file.

Link to comment
Share on other sites

I found it very usefull but with HTTP request i can do:Javascript to PHP to Javascript ?
Exactly. With JavaScript's XMLHttpRequest() object (a.k.a. AJAX) you can execute a PHP script with certain parameters, and get back the response to JavaScript, from which you can then do whatever you want with it (e.g. put it in a textarea).
in the sample i do not see a php file.
I told you the code for that is not trivial... Try it without any parameters first (just so that you see the flow in action). Request this file, and try to just fetch it's contents ("Contents of a generated XML file"):
<?phpheader('Content-Type: application/xml');echo '<root>Contents of a generated XML file.</root>';?>

If you can go that far, you can use the above mentioned PHP DOM API to fetch the exact portion of XSLT code you want, and output THAT with "echo" in the end.

Link to comment
Share on other sites

Great, well i was wondering if i could code it in JQuery and found out that $.ajax and $.GET function actually create an XMLHttpRequest() and therefore i can get the result of the php back to the javascript :), i didn't even know.So now i can do Javascript -> PHP -> Javascript. Thats very cool ! thanks for the tips. i think it will be easier, i won't have to deal with browser exceptions.

var url = "server.php";$.GET(url, {}, function(content){	 alert(content); //content = result of the php.});

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...