Accessing Nodes with Abbreviated Syntax - Exercise

Contact Us or call 1-877-932-8228
Accessing Nodes with Abbreviated Syntax - Exercise

Accessing Nodes with Abbreviated Syntax

Duration: 10 to 15 minutes.

This exercise is identical to the previous exercise except that you will be using the abbreviated syntax of XPath.

Please follow these steps.

  1. Open XPath/Exercises/XPathsAbbr.xsl for editing.
  2. This file contains three xsl:templates, one matching Head, Body, and Foot. In each template is a comment showing what the goal output is. You will use xsl:value-of tags and XPath to create this output.
  3. You may need to use some other XSL tags, such as the xsl:text tag.
  4. To test your solutions, transform XPath/Exercises/BusinessLetter2.xml against XPath/Exercises/XPathsAbbr.xsl.

Solution:

XPath/Solutions/XPathsAbbr.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<XPathTests>
			<xsl:apply-templates />
		</XPathTests>
	</xsl:template>
	<xsl:template match="Head">
		<XPathTest>
			<!--OUTPUT: 
			Mr. Joshua Lockwood is from the United States-->
			<xsl:value-of select="Recipient/Name/@Title"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="Recipient/Name/FirstName"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="Recipient/Name/LastName"/> is from the 
			<xsl:value-of select="Recipient/Address/Country"/>
		</XPathTest>
	</xsl:template>
	<xsl:template match="Body">
		<XPathTest>
			<!--OUTPUT:
			Bill Smith works at Webucator, Inc.
			His email is bsmith@webucator.com.
			If you have any questions, feel free to call me at 800-555-1000 x123.-->
			<xsl:value-of select=".//FirstName"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select=".//LastName"/> works at 
			<xsl:value-of select=".//Company"/>
			His email is <xsl:value-of select="Para/Email"/>
			<xsl:text>
			</xsl:text><xsl:value-of select="Para[last()]/text()[1]"/>
			<xsl:value-of select="Para/Phone"/>.
		</XPathTest>
	</xsl:template>
	<xsl:template match="Foot">
		<XPathTest>
			<!--OUTPUT:
			VP of Operations: Smith, Bill -->
			<xsl:value-of select=".//JobTitle"/>: 
			<xsl:value-of select=".//LastName"/>, 
			<xsl:value-of select=".//FirstName"/>	
		</XPathTest>
	</xsl:template>
</xsl:stylesheet>
Next