Question about XPaths and Javascript

By Albert on September 12, 2010 12:48 PM

I just posted this in response to a question about XPaths and Javascript on Stackoverflow:

I would look for an XSLT javascript library. Since most modern browsers have built-in XSLT support, and XSLT includes support for XPath, it is possible to use that engine to power your XPath selectors.

Personally, I've used Sarissa and the Glyphix jQuery.xslTransform libraries successfully:

* http://jquery.glyphix.com/
* http://dev.abiss.gr/sarissa/

This looks interesting too:

* http://johannburkard.de/software/xsltjs/

Serna XML Editor is Now Open Source

By Albert on January 25, 2010 8:18 AM

Good news - the free software community has a new contender for editing XML documents:

Serna Free - Open Source XML Editor

I haven't tried it yet, but the screenshots look nice. Thankfully the code is really free - no major hurdles to access the raw source. I went ahead and mirrored it at Docunext Mirrors, just in case:

Serna-free source code at Docunext Mirrors

Identity Template

By Albert on December 15, 2009 10:48 PM

An important part of learning XSL is understanding the identity template.

What is the "XSL Identity Template"?

An XSL identity template will transform an XML document into itself, meaning the input will be the same as the output.

It doesn't sound very important, but it actually is. The importance comes with the ability to change only a tiny bit of the document, or a selection of the document.

The identity template can be implemented many ways. Here's an example:

<!-- XHTML FRIENDLY IDENTITY TEMPLATE FOR ELEMENTS-->
<xsl:template match="node()">
  <xsl:element name="{name()}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<!-- XHTML FRIENDLY IDENTITY TEMPLATE FOR CONTENT AND ATTRIBUTES -->
<xsl:template match="@*|text()|processing-instruction()">
  <xsl:copy/>
</xsl:template>

<xsl:template match="comment()">
  <xsl:copy/>
</xsl:template>

Circular Processing with XSL

By Albert on December 9, 2009 1:17 AM

The idea is to feed the output of a stylesheet back into itself, but how can the process be managed, not infinite?

Andrew Welch suggests:

You can do all the transformations in your pipeline in one stylesheet by performing transformations within variables, with each variable operating on the previous one.

So, as top-level variables you could have:

<xsl:variable name="firstVar-rtf">
<xsl:apply-templates/>
</xsl:variable>
<xsl:variable name="firstVar" select="exsl:node-set($firstVar-rtf)"/>


<xsl:variable name="secondVar-rtf">
<xsl:for-each select="$firstVar">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="secondVar" select="exsl:nodet-set($secondVar-rtf)"/>


Here $firstVar operates on the source xml, and $secondVar works on the 'result' of the apply-templates in $firstVar.

The final link in the chain is of course:

<xsl:template match="/">
<xsl:for-each select="$lastVar">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>


All you need to do is separate out your problem into logical steps and perform each one in a varaible.

I do this a lot when xslt 1.0 struggles to do a task in one go, such as finding the average of two percentages written as 45% and 55%.

The first variable would translate() the '%' away, the second variable would find the average.

Dimitre notes:

This will select just the root node (/) of the temporary tree contained in $firstVar

Must be:

<xsl:for-each select="$firstVar/node()">
> <xsl:apply-templates/>
> </xsl:for-each>
> </xsl:variable>
> <xsl:variable name="secondVar" select="exsl:nodet-set($secondVar-rtf)"/>

> > Here $firstVar operates on the source xml, and $secondVar works on the
> 'result' of the apply-templates in $firstVar.
>

> The final link in the chain is of course:
>

> <xsl:template match="/">
> <xsl:for-each select="$lastVar">

The same problem:

Must be:

<xsl:for-each select="$lastVar/node()">

Dimitre Novatchev.

I'm busy trying to figure this out....

Didn't do what I wanted it to, but I did learn something.

<xsl:variable name="firstVar-rtf">
  First
  <xsl:apply-templates />
</xsl:variable>
<xsl:variable name="firstVar" select="exsl:node-set($firstVar-rtf)"/>

<xsl:variable name="secVar-rtf">
Second
  <xsl:for-each select="$firstVar/node()">
    <xsl:copy-of select="."/>
    <xsl:apply-templates />
  </xsl:for-each>
</xsl:variable>
<xsl:variable name="secVar" select="exsl:node-set($secVar-rtf)"/>

<xsl:variable name="tripVar-rtf">
Third
  <xsl:for-each select="$secVar/node()">
    <xsl:copy-of select="."/>
    <xsl:apply-templates />
  </xsl:for-each>
</xsl:variable>
<xsl:variable name="tripVar" select="exsl:node-set($tripVar-rtf)"/>

<xsl:template match="/">
<div>
  <xsl:for-each select="$tripVar/node()">

    <xsl:copy-of select="."/>
    <xsl:if test="name()='h2'">
    skdlj
    </xsl:if>

    lakjdfkj
  </xsl:for-each>
  </div>
</xsl:template>

Standard XSL Standard Templates

By Albert on December 8, 2009 12:03 PM

I'm working on a set of standard XSL templates for web application interface markup.

They are called 1bb02b59 hosted at github.com and are licensed using the Apache 2.0 license, though I may switch or add the BSD or MIT licenses.

I'm also thinking about aligning up with the web-app-theme project for CSS and Javascript integration, though that appears to be RoR-centric.

The README.markdown file:

Standard General XSL Templates

Output Templates

  • Choose HTML4 strict, XHTML 1.0 transitional, or XHTML 1.1 strict

Usage

XSL Namespace Aliases

By Albert on December 7, 2009 12:14 AM

I've been using XSL for a long time, and I know I've used XSL in the past to generate XSL, but today I found a very handy technique for doing so: namespace aliases!

Here's the articles I found which explain how its done:

http://www.xml.com/pub/a/2001/04/04/trxml/index.html

http://www.xml.com/pub/a/2003/11/05/xslt.html

In a nutshell, this is it:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xslo="http://www.w3.org/1999/XSL/TransformAlias"
>
<xsl:namespace-alias stylesheet-prefix="xslo" result-prefix="xsl"/>
<xsl:template match="/">
<xslo:stylesheet version="1.0">
<xslo:template match="/">
<xslo:value-of select="//name"/>
</xslo:template>
</xslo:stylesheet>
</xsl:template>


</xsl:stylesheet>

Categories