Interesting: greater-than character does not need to be escaped in XML
DOM events don't have the “on” prefix
To make matters worse, the attachEvent method (Microsoft equivalent of the addEventListener DOM method) expects the event names with the “on” prefix.
Serve SQL data in XML format
- Creating the XML markup in the server-side script
- Using the SQL server to generate the XML markup
- Using server-side XSLT transformation in combination with SQL server-produced XML data
Firefox hides the ?xml processing instruction
For example, when the following XML document ...
<?xml version="1.0" encoding="windows-1250"?>... is transformed into a DOM tree and processed with this JavaScript function:
<?xml-stylesheet href="countryPage.xsl" type="text/xsl"?>
<Test />
function testPI(dom) {
var topNodes = dom.childNodes;
for (var i = 0; i < topNodes.length; i++) {
var node = topNodes[i];
wr("nodeType="+node.nodeType);
if (node.nodeType == 7) {
wr("PI="+node.target+" ... "+node.data);
}
}... Internet Explorer generates the following text: nodeType=7... while Firefox skips the <?xml ?> processing instruction:
PI=xml ... version="1.0" encoding="windows-1250"
nodeType=7
PI=xml-stylesheet ... href="countryPage.xsl" type="text/xsl"
nodeType=1
nodeType=7
PI=xml-stylesheet ... href="countryPage.xsl" type="text/xsl"
nodeType=1
IP address to physical location mapping
Sometimes I would also like to figure out where the visitor causing the ASP error is coming from (more so if the errors are due to lack of resources, indicating unexpectedly high load). The usual tools (nslookup) fail if the ISPs don't enter their IP address space in reverse DNS lookup zones. In the past I've tried to use various Internet registries to figure out who the address is allocated to … and finally I've stumbled on the IP2Location web site, which does exactly what I was looking for: I enter the IP address and it tells me where the visitor is coming from (including a nice overview map for those who have no clue about geography).
Publish code samples in Blogger
use strict;
use Win32::Clipboard;
our $lcnt = 0;
our $result = "" ;
our $CLIP = Win32::Clipboard();
while (<>) {
if (/\n/) { chop; }
$_ =~ s/\&/&/gi;
$_ =~ s/\</</gi;
$_ =~ s/\>/>/gi;
while (/^\s/) { $_ =~ s/^(\s*)\s/$1 /gi; }
$result .= ($lcnt == 0 ? "<pre class='code'>" : "\n") ;
$result .= $_;
$lcnt ++;
}
$result .= "</pre>";
$CLIP -> Empty();
$CLIP -> Set($result);
print "Result is on the clipboard";
If you're not fluent in PERL, don't even try to understand what it does ... it works :)
Extract the default stylesheet from the DOM document
You can find a good case study on using XML and XSLT in my Inform-IT article Optimized Presentation of XML Content.
The following JavaScript function extracts the default XSLT stylesheet from the XML document (passed to the function as DOM Document object). It relies on the fact that the DOM Document object extends the Node object and thus has the childNodes property that contains the root element as well as all processing instructions.
function getXMLStylesheet(dom) {
var topNodes = dom.childNodes;
for (var i = 0; i < topNodes.length; i++) {
var node = topNodes[i];
if (node.nodeType == 7) {
if (node.target == "xml-stylesheet") {
var match = /href="(.*?)"/gi.exec(node.data);
if (match.index >= 0) return match[1];
}
}
}
}A typical usage of this function is illustrated below:
<script src="/sarissa.js"></script>
...
<script>
var xrq = new XMLHttpRequest();
xrq.open("GET","getPI.xml",false);
xrq.send(null);
if (xrq.status == 200) {
var dom = xrq.responseXML;
var xsl = getXMLStylesheet(dom);
alert("stylesheet="+xsl);
}
Constants of the DOM Node object are not implemented in Internet Explorer
if (node.nodeType == node.TEXT_NODE) { ... }
... which would be largely self-documenting thus has to be written as ...
if (node.nodeType == 3) { ... }
... unless you define the same constants in your JavaScript.
Use JavaScript Progressive Enhancement to simplify HTML table formatting
Should I encode data as XML attributes or child nodes
- If your data is simple and non-hierarchical (for example, results of SQL query), using XML attributes will reduce the response size (due to shorter markup)
- If you have repeating values in your data set, you cannot use attributes, as XML allows only a single attribute with a certain name per tag. In this case, you have to use child tags to encode repeating values. However, you can still transfer the actual value as an attribute of the child tag, not as text in the tag.
- If you're transfering large character fields, using child tags will increase the readability of the XML response when you're inspecting it with a browser (you know, sometimes we have to do a bit of debugging :)
- You might hit platform-dependent limitations if you use long attribute values, so yet again you would be better off using child nodes in this case
- Obviously, you cannot use CDATA section in an attribute, the attribute value has to be properly quoted.
This post is part of You've asked for it series of articles.
How do I use xsl:choose
The Google query strings bringing visitors to my blog are sometimes amazing, like the one in the title of this post. Let's start with a tangential remark: if you're serious about XSLT, you should invest into a good book (XSLT Programmer's Reference is one of the best technical books I've ever read). If, on the other hand, you're just looking for a quick fix, here's the whole story
XSLT does not have if-then-else or select-case constructs like most other languages. You can implement both functions with the xsl:choose element that can take as many xsl:when children as needed. These are evaluated sequentially, the first one that succeeds is executed. You can also use xsl:otherwise as the last child in the list to specify the default behavior.
This post is part of You've asked for it series of articles.
Find processing instructions in XSLT
<?user Web admin (admin@something.net) {00000000-0000-0000-0000-000000000000}?>To get this string into an XSLT parameter, I use the following XSLT instruction:<xsl:param name="username" select="/processing-instruction('user')" />A processing instruction can appear anywhere in the XML document. To find processing instructions that are not children of the root element, use the appropriate XPath syntax.
You have to use cells array, not DOM children to select table cells
<table>... the first child of the TR element is the whitespace between the TR tag and the first TD tag. As the whitespace is allowed in that position by the HTML standard, you cannot rely on the TR.firstChild property. You should use TR.cells[0] to select the first cell.
<tr id='test'>
<td>Cell #1</td>
<td>Cell #2</td>
</tr>
</table>
Introduction to HIJAX
How do you write utf-8 data from ASP?
Obviously this is too simple, so it's hard to find an explicit answer in the online documentation:
- To select the output encoding you want to use in the ASP script, you have to set the response.codepage property or change the per-page default with the @Codepage directive. To use utf-8 encoding, set the response.codepage to 65001.
- The HTTP Content-type header should match the encoding you're using. Set the response.charset (second part of the content-type header) to "utf-8".
- If you use META tag in your HTML to set the content-type or encoding attribute in the xml pseudo-instruction, these have to match the HTTP encoding as well. You don't have to specify utf-8 encoding in XML, as it's the default XML encoding.
- Instead of setting response.codepage in every response, you can set session.codepage at the start of the session.
- Important: If you use HTML forms, you have to set the @Codepage in all ASP scripts processing the forms or change the AspCodepage metabase property, otherwise the script might misinterpret the input data.
This post is part of You've asked for it series of articles.
