XSLT

Page  1  |  2  |- 3 -|  4  

 
 

In the previous examples, the XSLT command - value-of - has been used. However, these aren't called, functions, but rather, elements. The word - function - is used for certain built-in XPath commands, instead.

Let's see another XSLT example, and some more of these XSLT, elements:

<xsl:template match="stylesheet">
    <xsl:if test="text() != ''">
    <xsl:element name="LINK">
       <xsl:attribute name="REL">stylesheet</xsl:attribute>
       <xsl:attribute name="TYPE">text/css</xsl:attribute>
       <xsl:attribute name="HREF">
          <xsl:value-of select="vb:fGetRelURL(//file/*,string(.))"/>
       </xsl:attribute>
    </xsl:element>
    </xsl:if>
</xsl:template>

 

The if construct is like that in any language - though the if-then-else, as already seen, is had with xsl:choose and when. This is a template that is called, or 'fired', etc. only for elements in the XML that are named, stylesheet. And the purpose, here, is obviously to create a HTML tag like - <LINK REL="stylesheet" TYPE="text/css" HREF="some.css">.

 
Shorthand (Attribute Value Template)

 

There's a shorthand notation (called an attribute value template) that you can use to build something like this, rather than use the wordy method of XSLT attribute elements. And that is to simply write the HTML tag as text, and include the XPath references inside of curly braces. But it has to be an attribute value, even in the text, because the HTML, too, is being read and parsed. And XPath can only be used as an attribute value. And remember, too, empty elements must be closed, for example with the slash before the bracket (greater than sign).

<LINK REL="stylesheet" TYPE="text/css" HREF="{vb:fGetRelURL(//file/*,string(.))}" />

 
External Scripting

 

The HREF attribute, here, is being created by a call to a vb namespace that obviously includes functions - in fact, procedures written in vbscript. What's happening with that "vb" function call is that two arguments are being sent. Using XPath, all the elements under any file element will be sent as a flat list to the function. In this case, there's only one file element. So everything under that is sent to the function. The second argument is just the text() value, with the period shorthand here, of the particular stylesheet element which 'triggered' this template. The string() function makes sure that it's passed to the function as a string, rather than perhaps as a node or whatever else. The function is expecting a simple string type.

Msxml has a proprietary element/command which allows inclusion and communication with script languages like vbscript, and jscript. XSLT is a powerful language. And it could probably be used to accomplish some of these things. However, the scripts also allow for inclusion of library functions, ActiveX and whatever else. In the case of this function (which won't be shown here), a regular expression library is used.

So let's say there's a file - vbCom.xsl. And it starts out:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:vb="vbasic">
 
<msxsl:script language="vbscript" implements-prefix="vb">
<![CDATA[
 
' ____________________________________________________________
' Cover to fRelURL function, just below.
 
Function fGetRelURL(paths,strRelPath)
     Dim strFullSourcePath, strFullTargetPath
 
.
.
.
 
End Function
 
]]></msxsl:script>
 
</xsl:stylesheet>

 

This vbCom.xsl file can be thought of as a component that can be loaded, if required. You can have a whole collection or library of XSLT templates at the ready. XSLT has another element - xs:include - which allows loading of other xsl templates/programs. So in the XSLT (let's call it main.xsl) referred to, directly, by a particular XML document, one might include:

 
<?xml version="1.0"?>
 
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:vb="vbasic">
 
<xsl:include href="vbCom.xsl"/>
 
<xsl:template match="root">
 
.
.
.
 

 

You can see that the vb namespace is declared in both xsl documents, and that the implements-prefix is how the script is identified with that namespace. The CDATA section, with the brackets just so, is similar to a <pre> section in HTML - an unparsed section that's taken as is.

XSLT is a 'declarative' language. Variables aren't allowed as with vbscript, above. But they can be used, nonetheless, via recursion.

Read on.