If you aren’t in the habit of using XSLT transformations to
take your data content and produce your layout, you might want to look into the
power of XSLT. One of the best advantages of using XSLT to control your layout
is that you aren’t restricting your data to one particular layout.
If you keep your data and business logic separate from your
layout, you can create different XSL style sheets to produce the different
layouts you need. This is particularly helpful if you’re building user
interfaces for different layout types to handle browser-to-browser
incompatibilities. It’s also useful if you’re working with different
technologies such as WML or Web services, as opposed to regular HTTP requests.
One drawback that developers without much experience in
transformations might encounter is the sense that the XSL is limited to the
available XML for the transformation—but that’s not the case. An easy way to
add functionality is through the use of the <param>
XSLT element. This element allows you to add a parameter to your style sheet
that you can use to pass in all sorts of information. Once the XSL style sheet
is in memory, you can set the parameter’s value using XPath.
The style sheet is, after all, another DOM document. If you don’t have XPath available to you, you might want to check out getElementsByTagName in the DOM 1 specification.
Let’s say you have a parameter with the name
name=”browser”/>. Before you perform your transformation, you want
to set that parameter to a value that the style sheet will use in conditions to
produce the appropriate output. Simply set the text to be a value that you can
use in a condition statement.
shows you how in ASP.
displays how your XSL style sheet should look.
If you examine the ASP code, you’ll see that I’ve selected
the <param> XSLT element using XPath: //xsl:param[@name=’browser’]. This says, “Get me the first xsl:param node that has a name
attribute that’s equal to ‘browser’.” The element’s text is then set to ‘ie’. When you inspect the XSLT, you’ll see the xsl:param element, as well as how
the parameter is used in an xsl:choose block. You can take all of this a bit further and create a function
to handle all of your XSLT transformations.
View Listing C.
When you use this function, you pass in your last parameter,
pArrayOfParams, as an array of arrays. An example
structure would be: ((“browser”, “ie”),
(“version”, “6.0”)). The outer array contains two array
elements. The first array element is an array that contains two array elements.
The first element is the name of the parameter (the name attribute on the xsl:param node), and the second
element is the value. The second element of the outer array is the same
structure as the first inner array. This allows you to pass in as many
parameters as you need for your transformation.
If you’d like more examples in PHP and Mozilla
Keep your developer skills sharp by automatically signing up for TechRepublic’s free Web Development Zone newsletter, delivered each Tuesday.