Select XML elements in XSLT template
Text nodes in XSLT
In the example mentioned by Alexandar …
<input type="submit">… a single text node within the xsl:attribute element (as parsed by XML parser) includes the Login text as well as a few whitespace characters. To get rid of the extra whitespace characters (it looks like the target application hates having leading whitespaces in submit button names), you could write the whole value in-line (now there is no extra whitespace in the attribute value):
<xsl:attribute name="value">
Login
</xsl:attribute>
</input>
<xsl:attribute name="value">Login</xsl:attribute>Alternatively, you could split the contents of the xsl:attribute element into whitespace-only text nodes (which are not copied to the output) and a non-whitespace text node (containing the Login text). The easiest way to do it is to insert an xsl:text element within the xsl:attribute element:
<xsl:attribute name="value">
<xsl:text>Login</xsl:text>
</xsl:attribute>
This post is part of You've asked for it series of articles.
Search Engine Optimization in XML+XSLT designs
- Google can process XML data, but only stores it into supplementary index (lower rankings)
- If the server's XML output does not contain enough context (for example, product description in HTML-ish format), the search engines cannot make any sense out of it, so it would not be indexed appropriately.
- Search engines will not follow explicit (let alone implicit) links in XML documents, so you need a sitemap (classic HTML page, Atom/RSS feed or Google sitemap) to help search engines find the content pages
Dynamic output elements in XSLT
<book title="Sample book">… we'd like to generate HTML H1 heading for the chapter and H2 … Hx headings for the sections. It's obvious that the heading element's name can be computed easily as 'H'+number-of-ancestors. The corresponding XSLT template is of course a bit more complex and uses the xsl:element tag to generate the output element.
<chapter title="First chapter">
<section title="Section in first chapter">
<section title="Chunk of text within a section">
<para>My paragraph</para>
</section>
</section>
</chapter>
</book>
<xsl:template match="chapter|section">
<xsl:element name="{concat('h',count(ancestor::*))}">
<xsl:value-of select="@title" />
</xsl:element>
<xsl:apply-templates />
</xsl:template>
The xsl:element expects a QName (a string specifying the element's name) as its name attribute. To insert dynamically computed element's name, you have to use curly braces around the expression.
Add a truly modal dialog box to your web application
XSLT global replace
<xsl:template name="globalReplace">The xsl:choose is the if-then-else/select replacement. We test if the source substring is present in the value …
<xsl:param name="value"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>… if it is, we do tail recursion first, replacing all the remaining values and storing the result in $rest …
<xsl:when test="contains($value,$from)">
<xsl:param name="rest">… and then concatenate the substring before the first from value with the to value and the results of the tail recursion.
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="substring-after($value,$from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:param>
<xsl:value-of select="concat(substring-before($value,$from),$to,$rest)" />If the source string does not contain the substring to be replaced, we just return it (this also ends the tail recursion) …
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$outputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
A few must-read JavaScript documents
XSL: Copy input elements into output stream
<catalog>… and use a simplistic approach to the copy-of …
<product id="123">
<name>Sample product</name>
<text>A <strong>marvellous</strong>
<span class='red'>red</span> book</text>
</product>
</catalog>
<xsl:template match="product">… you'd end up with an extra non-HTML-compliant text node in the output stream …
<h3><xsl:value-of select="name" /></h3>
<xsl:if test="text">
<xsl:copy-of select="text" />
</xsl:if>
</xsl:template>
<h3>Sample product</h3>To get just the contents you'd like to get, you should still use the xsl:copy-of instruction, but select just the child element (child::*) and the text nodes (text()) of the text element. To add icing on the cake, we'll enclose the contents of the text element in a P tag:
<text>A <strong>marvellous</strong>
<span class="red">red</span> book </text>
<xsl:template match="product">
<h3><xsl:value-of select="name" /></h3>
<xsl:for-each select="text">
<p><xsl:copy-of select="text() | child::*" /></p>
</xsl:for-each>
</xsl:template>
Displaying element's ancestors
<book title="Sample book">
<chapter title="First chapter">
<section title="Section in first chapter">
<section title="Chunk of text within a section">
<para>My paragraph</para>
</section>
</section>
</chapter>
</book>
The section elements can be nested
You could use the following template to display the breadcrumbs in front of each heading:
<xsl:template match="chapter|section">
<p class='crumbs'>
Walk through all the ancestors, starting from the root element
<xsl:for-each select="ancestor::*">
If the current element has an ancestor, we've obviously printed something already, so insert a breadcrumb separator …
<xsl:if test="ancestor::*"><xsl:text>, </xsl:text></xsl:if>
… and display the title of the ancestor element.
<xsl:value-of select="@title" />
</xsl:for-each>
</p>
At the end, display the current heading and recursively process child elements.
<h2><xsl:value-of select="@title" /></h2>
<xsl:apply-templates />
</xsl:template>
Max function in ASP
Function Max(A,B)Things get a bit more complex if you want to take in account the NULL values (assuming non-NULL is always greater than NULL):
Max = A : If B > A Then Max = B
End Function
Function Max(A,B)However, I prefer the multi-value function that can take as many arguments as you like (unfortunately in an array, as VBScript functions can't take variable number of arguments):
Max = A : If IsNull(B) Then Exit Function
If B > A Or IsNull(A) Then Max = B
End Function
Function MaxArray(A)
Dim I
MaxArray = A(LBound(A))
For I = LBound(A) + 1 To UBound(A)
If A(I) > MaxArray Or IsNull(MaxArray) Then MaxArray = A(I)
Next
End Function
The code uses a VBScript quirk: function name without parenthesis when used within the function refers to the current function's value, not a recursive call
To call the MaxArray function, you don't have to construct an array beforehand, you can also use the array VBScript function, for example:
mxv = MaxArray(array(A,B,C,D))
Keyboard shortcuts in web user interface
Firefox vulnerabilities when using data: or jar: protocols
XSL function to find parent node name
- The local-name function returns the name of its argument (without the namespace) or the name of the current element if it's called without an argument;
- The parent:: axis selects the current element's parent;
- The parent::* matches any node that is the parent of the current element.
<book>… this XSL template will output “My parent is: section”
<chapter>
<section>
<title>My section</title>
<para>My paragraph</para>
</section>
</chapter>
</book>
<xsl:template match="para">
My parent is: <xsl:value-of select="local-name(parent::*)" />
</xsl:template>
This post is part of You've asked for it series of articles.
Fading background made simple
function setFading (e) {
e.style.backgroundColor = "#FFFFFF";
if (!e.xa) e.xa = new xAnimation();
e.xa.rgb(e,"background-color","#EDC226",1000,1,1);
}
Emulating activeElement property with DOM browsers
All the solutions I've found with Google were kludges along the lines of “scan all the elements and test each one to see whether it has focus”. It's much better to use the capture phase of the DOM event model and track the focus and blur events with the document object:
function _dom_trackActiveElement(evt) {
if (evt && evt.target) {
document.activeElement =
evt.target == document ? null : evt.target;
}
}
function _dom_trackActiveElementLost(evt) {
document.activeElement = null; }
if (document.addEventListener) {
document.addEventListener("focus",_dom_trackActiveElement,true);
document.addEventListener("blur",_dom_trackActiveElementLost,true);
}This implementation is slightly over-pessimistic; if the browser window loses focus, the activeElement is set to null (as the input control loses focus as well). If your application needs the activeElement value even when the browser window doesn't have the focus, you could remove the blur event listener.
