Académique Documents
Professionnel Documents
Culture Documents
Source:http://www.wschools.com
XSLT Tree Structure
Feedback.xml
<module>
<studentemail>s3@uom.ac.mu</studentemail>
<modulename code="CSE1041">Web Tech I</modulename>
<moduleyear>2010</moduleyear>
<classsize>Adequate</classsize>
</module><module>
<studentemail>s3@uom.ac.mu</studentemail>
<modulename code="CSE2041">Web Tech II</modulename>
<moduleyear>2010</moduleyear>
<classsize>Adequate</classsize>
</module></feedback>
Examples of Expressions
feedback
selects all child nodes of the feedback element
returns module
/feedback
Selects the root element, feedback
//module
Selects all module elements
Xpath Expressions
• The primary syntactic construct in XPath is
the expression.
• These path expressions are to select nodes
or node-sets in an XML document.
• An expression is made of 3 parts:
An axis
A node test
An optional predicate
XPath Expressions(2)
The node test is used to specify what type of
node in the axis should be selected.
For example, to select all child element nodes
of feedback element nodes that are module
element nodes, you could write the following:
/feedback/module
The initial / character indicates that the context
node is the root node.
XPath Expressions(3)
/feedback/module
The next location step, feedback, selects all
feedback element nodes in the child axis.
The second location step is module, which
selects module element nodes in the child
axis.
The | can be used to select more than one path
in XPath
e.g. //module/studentemail | //module/classsize
Selects all the studentemail AND classsize elements of
all modules elements
Predicates
The syntax:
<xsl:if test="expression">
... ...
some output if the expression is true
... ...
</xsl:if>
Using a condition with the data
…
<table><tr bgcolor="#9acd32">
<th>Student Email</th> <th>Module Name</th>
<th>Year</th></tr>
<xsl:for-each select="feedback/module">
<xsl:if test="moduleyear > 2009">
<tr> <td><xsl:value-of select="studentemail"></td>
<td><xsl:value-of select="modulename"/></td>
<td><xsl:value-of select="moduleyear"/></td>
</tr></xsl:if></xsl:for-each></table>…
Viewing feedback.xml in firefox
The <xsl:choose> Element
The syntax:
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
Using multiple conditions with
the data
<xsl:for-each select="feedback/module">
<tr><td><xsl:value-of select="studentemail/."/></td>
<td><xsl:value-of select="modulename/."/></td>
<xsl:choose>
<xsl:when test="moduleyear > 2009">
<td bgcolor="#ff00ff"><xsl:value-of
select="moduleyear/text()"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="moduleyear/."/></td>
</xsl:otherwise>
</xsl:choose> </tr></xsl:for-each> </table>
Viewing feedback.xml in firefox
Multiple <xsl:when> tags
<xsl:choose>
<xsl:when test="moduleyear > 2009">
<td bgcolor="#ff00ff"><xsl:value-of
select="moduleyear"/></td>
</xsl:when>
<xsl:when test="moduleyear > 2007">
<td bgcolor="#cccccc"><xsl:value-of
select="moduleyear"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="moduleyear"/></td>
</xsl:otherwise> </xsl:choose>
Viewing feedback.xml in firefox
Default or fixed values