> DNU home > online course > DirXML Driver Development
April 2001
DeveloperNet University Course

XSLT and XPath Programming

Now that we understand how stylesheets are used in DirXML, we will demonstrate how to program one to solve the telephone number transformation problem described above.

Note: This module is only intended to give you a general understanding of the usage of XSLT and XPath. For a more detailed understanding, refer to the XSLT and XPath specifications available from the W3C web site.

A PBXSimulator XDS document describing the creation of an account is shown in Code Snippet 1. This is a typical input document for the input stylesheet in the PBXSimulator driver.


<nds dtdversion="1.0" ndsversion="8.5">
<source>
<product asn1id="2 16 840 1117191x" version="1.0">DirXML</product>
<contact>Novell, Inc.</contact>
</source>
<input>
<add class-name="User" src-dn="0199">
<association>0199</association>
<add-attr attr-name="firstName">
<value type="string">John</value>
</add-attr>
<add-attr attr-name="lastName">
<value type="string">Smith</value>
</add-attr>
<add-attr attr-name="phone">
<value type="telenumber">555-555-1234</value>
</add-attr>
</add>
</input>
</nds>

Code Snippet 1: PBXSimulator XML/XDS Tree to Add an Account.

Note: Given the create rules defined in the base lab, the event described by this document would be vetoed because it does not contain the 'OU' or 'L' attributes. However, for the purpose of this discussion, we will assume that this XDS document would be allowed to pass and would be implemented by the DirXML engine to create an object named John Smith in NDS.

Notice that the phone number value in the XDS document in Code Snippet 1 is delimited by hyphens, just as it would come from PBXSimulator. The job of the XSLT/XPath program we are going to examine is to capture this value and modify it so that it is delimited by dots instead of hyphens.


Figure 1: Major functional elements in DirXML stylesheet processing.

There are three main stages in XSLT processing.

  1. First, the XSLT stylesheet document is read from an NDS stream attribute. The stylesheet document is parsed by an XML parser into tree form. The XSLT processor processes this tree form and constructs a compiled internal rules representation. This initialization occurs one time, at start-up.
  2. Second, the input document is parsed into a node tree. Figure 2 is a node tree diagram showing how the XSLT processor looks at the PBXSimulator add account input document shown in Code Snippet 2.

    Before proceeding, examine the input document in Code Snippet 1 and the node diagram in Figure 2 until you thoroughly understand the relationships between the two.

  1. Third, the processor applies the rules from the stylesheet to the input document node tree to build a resulting output document.

    Note: Stage one occurs only at initialization. Stages two and three occur for every input document processed for the stylesheet.


Figure 2: Node Tree Diagram For Code Snippet 1 Input Document.

Code Snippet 2 is an XSLT/XPath stylesheet solution for the telephone transformation problem we described earlier. The following discussion is divided into two stages. The first stage describes the code as it relates to stylesheet rule initialization. The second describes the code as it relates to the processing of input document node trees against the stylesheet rules to produce output documents.


1.   <?xml version="1.0" encoding="ISO-8859-1" ?>
2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" >

3. <xsl:template match="/">
4. <xsl:apply-templates select="node()|@*"/>
</xsl:template>

5. <xsl:template match="node()|@*">
6. <xsl:copy>
7. <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

8. <xsl:template match="add-attr[@attr-name='phone']">
9. <add-attr attr-name="phone">
10. <value type="teleNumber">
11. <xsl:value-of select="translate(.,'-','.')"/>
</value>
</add-attr>
</xsl:template>
</xsl:stylesheet>

Code Snippet 2: XSLT Stylesheet for PBXSimulator Phone Delimiter Xform.

Previous Contents Next
download sample file