Home > SOA Tips > XML Developer > XSLT expression processing techniques
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

XML DEVELOPER

XSLT expression processing techniques


Ed Tittel
09.20.2005
Rating: -5.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


In this sixth of nine pieces on the subject of XSLT, we'll take the various functions and expressions you've learned about in previous tips and describe how to embed them inside sequences of processing instructions. Usually, this occurs within the scope of some <xsl:call-template> markup element or another, since that element fulfills the same kind of role in XSLT that function or subroutine calls serve in many programming languages. In that particular context, in fact, you'll find most of the iterative and recursive structures in XSLT put to work—as illustrated in the labeled paragraphs that follow next.

Navigating to and processing specific document nodes
To use <xsl:call-template> to process some node other than the current document node, the most expeditious technique is to nest an <xsl:call-template> element inside an <xsl:for-each> element, but you can also provide a target template with a unique node name, and use the <xsl:apply-template> element to apply to some specific node instead. The "for-each" construct, however, is purely iterative and makes more sense in circumstances where all nodes in a document or results tree need to be processed anyway. You'll find examples of this approach completely spelled out in Michael Kay's XSLT Programmers Reference (Wrox, 2001, pp. 169 and 170). He also explains a neat technique on page 169 for calling <xsl:call-template> inside an <xsl:variable> element, where a simple conversion of the results tree to a string acts just as if the <xsl:call-template> element is returning a result from its invocation.

The structure for this sort of process may be illustrated as follows, where a template for enclosing string values within square brackets is called inside a variable element:

<!-- start with template definition, proceed to variable
     element -->
<xsl:template name="bracketit">
  <xsl:param name="string" />
  <xsl:value-of select="concat("'[', $string, ']')"/>
</xsl:template>

<xsl:variable name="cite-in-brackets">
  <xsl:call-template name="bracketit"/>
    <xsl:with-param name="string" select="@cite"/>
  </xsl:call-template>
</xsl:variable>

If the value of the cite attribute is Horn79, the resulting value of the variable cite-in-brackets will be [Horn79].

Using Recursion to Process Item Lists
XSLT lacks updatable variables like the counters used in conventional programming languages to progress through specific counts of activities, nor does it support conditional looping constructs such as while, for, or until either. But XSLT does support recursion, which provides a handy way to achieve the same sorts of purposes. The approach to this process involves setting up a recursive function call where the function is applied to the first node in a list or the first non-empty character sequence in a string, and the function is also applied to the remainder of the node list or string, with the first element or character sequence removed from its contents. For each type of data, operations proceed until the list or string is empty—namely, when no more nodes or non-empty strings occur in the remainder portion. In his very nice Sybex book titled Mastering XSLT, Chuck White refers to this approach as "…repeating a result fragment for every instance of a named expression" (2002, pg 266; he also has great markup examples in chapter 9, where recursion is covered on pp. 268-269, as Michael Kay does on pp. 168-175 of his already-cited book).

Managing Conditional Processing
The <xsl:choose> and <xsl:when> elements permit conditional branching where multiple alternatives might be needed; an <xsl:otherwise> element even permits explicit handling of item that fail all specific tests defined. The basic syntax provides a construct that looks every much like a typical if-then-else statements with arbitrary nesting. White spells out the basic syntax stripped of actual values or checks for such statements as follows on p. 291 of Mastering XSLT:

<xsl:choose>
  <xsl:when test="expression">
     <!-- processing statements -->
  </xsl:when>
  <xsl:otherwise>
     <!-- processing statements -->
  </xsl:otherwise>
</xsl:choose>

Given the various techniques that XSLT supports so readily, it's reasonably easy to build your own document processing examples around navigation, recursion, and conditional testing techniques. You can also revisit the XSLT tutorial at zvon.org for more illustrations and examples as well, or check out Sal Mangano's XSLT Cookbook (O'Reilly, 2003) for more elaborate and lengthy examples that you can import and easily adapt for your own uses.

About the author
Ed Tittel is a full-time writer and trainer whose interests include XML and development topics, along with IT Certification and information security topics. E-mail Ed with comments, questions, or suggested topics or tools for review.


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
XML Developer
Use the soapUI software tool to tame WSDL
WSDL 2.0, new messaging for Web services
Using RELAX NG For data integration
Efficient XML Interchange tackles data verbosity
XML to DDL imports, synchronizes database schemata
The basics of MathML 3.0
Migrating to XSLT 2.0
What's up with XML 2.0?
Say hello to XPath 2.0
Podcasting software covers many bases

XML and XML schema
What's the future of XML?
SOA pattern of the week (#7): policy centralization
Try XML-based Extensible Business Reporting Language (XBRL) for accounting reports
What's new at the W3C
Ganymede: Modeling tools target SOA, UML
Data services mashups emerge for SOA
Making sense of data services mashups
XML turns 10
SOA helps save 100-year-old business
Oracle maps heterogeneous data services strategy for SOA

XML security
Verizon uses BPEL app to cut down on code, check for fraud, and go green
Layer 7 adds SPARC
Oracle maps heterogeneous data services strategy for SOA
Partnership aims at governance for SOA and Web 2.0
SOA, Web services create software security challenges
Efficient XML Interchange tackles data verbosity
XML to DDL imports, synchronizes database schemata
The case against WS-Security
Layer 7 offers SOA 'virtual soft-appliance'
XQuery 1.0: A long time coming, now what?

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
class diagram  (SearchSOA.com)
Fast Infoset (FI)  (SearchSOA.com)
GeoRSS  (SearchSOA.com)
Keyhole Markup Language  (SearchSOA.com)
RELAX NG  (SearchSOA.com)
state diagram  (SearchSOA.com)
Universal Business Language  (SearchSOA.com)
Vector Markup Language  (SearchSOA.com)
XML infoset  (SearchSOA.com)
XML pipeline  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts