How to validate XSL file? - validation

could anyone help me with XSL file validation? I don't want to validate it for start/closed tags but for "inside atributes".
For examle:
My XSL file contains line <td><xsl:value-of select="year"/></td>
I need check this one for syntax errors like <td><xsl:vae-of select="year"/></td>.
Is there any way how to do this with javascript or jQuery?

An XSLT processor checks the stylesheet code so in Mozilla browsers you could pass a DOM document with your stylesheet code to the importStylesheet method of an XSLTProcessor object e.g.
var proc = new XSLTProcessor();
var sheet = new DOMParser().parseFromString([
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">',
'<xsl:template match="/"><xsl:vae-of select="year"/></xsl:template>',
'</xsl:stylesheet>'
].join('\n'), 'application/xml');
try {
proc.importStylesheet(sheet);
}
catch(e) {
// handle error here e.g.
console.log(e.message);
}
outputs "Component returned failure code: 0x80600001 [nsIXSLTProcessor.importStylesheet]".
But be aware that an XSLT 1.0 processor is supposed to process stylesheet code with a version greater than 1.0 in forwards compatible mode and that way if you wanted to validate user input code as XSLT 1.0 you would need to make sure that it has version="1.0" to catch any errors against the version 1.0 of the XSLT language.
And of course that error message is not very helpful to identify what's wrong in the code.

Related

Accessing Oracle ATG variables with Javascript

I am trying to pass the contents of a bean to javascript so that I can parse it and create a JSON object... (Yes I am still on ATG 9.1). However I am having trouble getting from serverside to client side.... I am new with this stuff and would appreciate any explanation as documentation on this is scarce and not helpful.
<dsp:tomap var="cartMap" bean="MyShoppingCartModifier.order" recursive="true"/>
<script>
var myCartMap = "${cartMap}";
//Logic (easy)
</script>
Doing this generates an "Uncaught SyntaxError: Unexpected token ILLEGAL" on my browser (Chrome)
Any wisdom will greatly help me in my quest in learning this stuff.
The problem is your usage of the tomap tag. You can't just pass in an entire tomap'd object because the tomap tag isn't going to create a nice, parsable json object.
You should either:
1) Format the json yourself right within your tags. Choose only the values that you want from the order.
<script>
var myCart = {
total : '<dsp:valueof bean="MyShoppingCartModifier.order.priceInfo.total">'
...
}
// Then use myCart for something here
</script>
or 2) There's a little known JSP to JSON library found here, http://json-taglib.sourceforge.net, that is very useful. To use that, you'd create a separate page, something like orderJSON.jspf, that is used to generate a pure json object from your order. Then in the page that you require this js, you can do:
<script>
var myCart = <%# include file="/path/to/orderJSON.jspf" %>
// Then use myCart for something here.
</script>

Why do XSLT inserted scripts behave like asynchronous in Firefox?

I am using in-browser XSLT to generate HTML, <script> included. Firefox apparently behaves oddly in this case.
The files below can be obtained and tried from there. (Open doc.xml and look at the log messages in the console.)
Below is the test XSLT stylesheet I made. The XML source doc.xml is simply <?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?><doc/>. The transform creates both an inline and an external script:
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<head>
<title>Firefox and XSLT inserted scripts</title>
</head>
<body>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
console.log('inline, asynchronous? '+ document.currentScript.async);
// -> "inline, asynchronous? false"
// alert('inline, pause');
function errorHandler(img) {
img.src = 'http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32-noshadow.png';
}
try {
document.write("<p>Inserted</p>");
} catch(e) {
console.error(e.name + ": " + e.message);
// -> "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
}
</script>
<p><img src="about:blank" onError="window.errorHandler?errorHandler(this):console.log('Too late for handling error!')"/></p>
</body>
</html>
The external script script.js is almost the same as the one inline:
console.log('external, asynchronous? '+document.currentScript.async);
// -> "external, asynchronous? false"
// alert('external, pause');
try {
document.write("<p>Inserted</p>");
} catch(e) {
console.error(e.name + ": " + e.message);
// -> "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
}
The console displays:
19:21:14.435 "Too late for handling error!" index.xml:1
19:21:14.477 "external, asynchronous? false" script.js:1
19:21:14.478 "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
script.js:7
19:21:14.479 "inline, asynchronous? false" index.xml:1
19:21:14.480 "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
index.xml:10
At first, the scripts seem synchronous, since async is false.
But in reality, they do behave asynchronously!
One cannot use document.write.
Callbacks (errorHandler() in my example) aren't immediately available.
Does anyone has a clue about what is happening here?
Mozilla's XSLT processor implementation creates a result tree and renders that directly so using document.write is not supported, see https://developer.mozilla.org/en/docs/XSL_Transformations_in_Mozilla_FAQ#What_about_document.write.3F. Other client-side XSLT processor implementations might feed the serialized transformation result to its HTML parser, in that case document.write works.
If you want to output stuff with your scripts inside XSLT then you need to use the DOM methods to create nodes and insert them. See http://home.arcor.de/martin.honnen/xslt/test2013122801.xml for an example.

jQuery GET html as traversable jQuery object

This is a super simple question that I just can't seem to find a good answer too.
$.get('/myurl.html', function(response){
console.log(response); //works!
console.log( $(response).find('#element').text() ); //null :(
}, 'html');
I am just trying to traverse my the html response. So far the only thing I can think of that would works is to regex to inside the body tags, and use that as a string to create my traversable jQuery object. But that just seems stupid. Anyone care to point out the right way to do this?
Maybe its my html?
<html>
<head>
<title>Center</title>
</head>
<body>
<!-- tons-o-stuff -->
</body>
</html>
This also works fine but will not suit my needs:
$('#myelem').load('/myurl.html #element');
It fails because it doesn't like <html> and <body>.
Using the method described here: A JavaScript parser for DOM
$.get('/myurl.html', function(response){
var doc = document.createElement('html');
doc.innerHTML = response;
console.log( $("#element", doc).text() );
}, 'html');
I think the above should work.
When jQuery parses HTML it will normally strip out the html and body tags, so if the element you are searching for is at the top level of your document structure once the html and body tags have been removed then the find function may not be able to locate the element you're searching for.
See this question for further info - Using jQuery to search a string of HTML
Try this:
$("#element", $(response)).text()
This searches for the element ID in the $(response) treating $(response) as a DOM object.
Maybe I'm misunderstanding something, but
.find('#element')
matches elements with an ID of "element," like
<p id="element">
Since I don't see the "tons of stuff" HTML I don't understand what elements you're trying to find.

ASP.NET MVC 3 Parse simple html tag/extract attribute value

What is the best way to parse html tag like that:
Results
i just need to extract a href value.
I need to do this on controller.
I know i may use linq to xml for example or regex.
Any ideas what is the better way for that case?
May be there is any MVC helpers ready to go?
Basically what i need to do:
I have my extension method witch returning current url
public static string GetCurrentUrl(this ViewContext context)
{
string action = (string)context.Controller.ValueProvider.GetValue("action").RawValue;
string controller = (string)context.Controller.ValueProvider.GetValue("controller").RawValue;
if (action.ToLower() != "index")
return String.Format("/{0}/{1}", controller, action);
else if (action.ToLower() != "index" && controller.ToLower() != "home")
return String.Format("/{0}", controller);
else
return "/";
}
I need to compare this url with the value from a href like that Results
Use a XML parser and avoid regex. For this specific case XDocument seems easy enough:
var doc = XDocument.Parse("Results");
var href = doc.Element("a").Attribute("href").Value;
For more complex scenarios when HTML specific parsing is required and manipulating the DOM you could use HTML Agility Pack.
parsing html is notoriously difficult, there are so many hidden gotchas. The HTML Agility pack, which is a .NET HTML parsing library. Fizzler enables css selectors for html in c# and is built on top of the agility pack.

How to get xml data using ajax in jquery?

I want to use ajax in jquery to get data for my page...
The problem is that the url which i call has some query strings to be sent along with it...
for example: the url which i call for getting data is:-
http://mysite.in.dataengine.aspx?t=abcde&token=h34jk3&f=xml
the data i get in response from this url can be in xml format or java script arrays(whichever i choose)
for eg...the xml wil look like this:-
<root version="1.0">
<Regions>
<Region SubCode="MWEST" RCode="west"/>
<Region SubCode="MCENT" RCode="north"/>
<Region SubCode="THAN" RCode="south"/>
</Regions>
</root>
and the javascript array would look like this :-
Region = new Array();
Region.push(new Array('MWEST', 'west'));
Region.push(new Array('MCENT', 'north' ));
Region.push(new Array('THAN', 'south'));
So when i get the data i want to store it in a drop down box.(using ajax)
Note I can get either xml OR javascript arrays as the returned data, not both together.
You can make an ajax call along with parameters like this:
var paramsData = "t=abcde&token=h34jk3";
$.ajax({
type: "GET",
url: "dataengine.aspx",
data: paramsData,
dataType: "xml",
success: function(xml){
//process xml from server
}
});
I would suggest you to get the data in JSON format, as Json comes natively to javascript and it much easliy manipulated using javascript as compared to XML. The easiest way i can see to work on your problem is to store all your data whether xml or json & put it inside a hidden div and then use jQuery to populate that data in a drop down box.
Here is an amazing jquery plugin with example that should ease your work
http://plugins.jquery.com/project/jqueryclientdb
Just parse it. I"m not sure if this will work, but it might:
xml = ...
region = new Array();
$(xml).find('Region').each(function() {
region.push(new Array($(this).attr('SubCode'), $(this).attr('RCode'));
});
Thanks for your help guys...but i have found the solution....Like i said...that i get in return either xml or javascript array...So..i'm using javascript arrays.. and using a function in jquery*($.getScript)* which fetches an external javascript code via ajax...Thus i am getting all my data now through ajax in jquery...

Resources