4 - Attributes

XML Document
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="iliad04.xsl"?>

<document>

<header>
     <title>Iliad</title>
     <book>1.</book>
     <verses>1-7</verses>
     <translator>Lance Jenott</translator>
     <date>
          <month>April</month>
          <year>2004</year>
     </date>
</header>

<poem>
     <line num="1">Sing, oh goddess, about the wrath of <name>Achilles</name> son of <name>Peleus</name>,</line>
     <line num="2">the accursed wrath which cast a myriad of pain upon the <name>Achaeans</name></line>
     <line num="3">and sent forth many noble souls of heroes to <name>Hades</name>,</line>
     <line num="4">making them prey for dogs and all birds.</line>
     <line num="5">The will of <name>Zeus</name> was fulfilled,</line>
     <line num="6">when, in fact, the two men first parted ways in strife,</line>
     <line num="7">the son of <name>Atreus</name>, ruler of men, and god-like <name>Achilles</name>.</line>
</poem>

</document>
This XML example has two new features: (1) all proper names of the characters have been tagged with the tag <name>, and (2) tag attributes have been added to both the <line> and <name> tags. (Tag attributes here are colored green.) Attributes can be used to provide even more specific information about the text deliminated by the tag, and like the XML tags themselves, attributes can be anything the document owner desires. For example, each of the <line> tags in this example contains a num attribute whose value is the poem's line number. Using an XSLT script, the attribute information can be used for such purposes as searching the document for specific information and/or displaying only select portions of the document. See the XSLT example below.


XSLT script: iliad04.xsl
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">

<table border="1" cellpadding="3">
	<th>Name of Character</th>
	<th>Line #</th>
	<xsl:for-each select="document/poem/line/name">
		<tr></tr>
		<td>
			<xsl:value-of select="current()"/>
		</td>
		<td>
			<xsl:value-of select="../@num"/>
		</td>
	</xsl:for-each>
</table>

</xsl:template>

</xsl:stylesheet>
Similar to Page 3 above, this script uses <xsl:for-each> and <xsl:value-of> to locate and display each instance of a <name> tag. The commands are set within an HTML table for formatting.

<xsl:value-of select="../@num"/>
The XPath information in the quotes ("../@num") points to the num attribute of the current <line> tag. Attributes are always specified with the character @. Since the <xsl:for-each> command is operating on the XPath level document/poem/line/name, the "../" tells the script to look one level up the XPath for the num attribute of the current <line> tag.


RESULTS of iliad04.xsl
Name of Character Line #
Achilles1
Peleus1
Achaeans2
Hades3
Zeus5
Atreus7
Achilles7
Each instance of a name is displayed along with the line number on which it is found.