Back
Next
XSLT element of <sl:attribute> can be used to add attributes to result elements whether created by
accurate result
elements in the style sheet or by instructions such as <xsl:element>. The expanded-name of the attribute to be
created is specified by a required name of attribute and an optional namespace attribute. Instantiating an
<xsl:attribute> element adds an attribute node to the containing result element node. The content of the
<xsl:attribute> element is a template for the value of the created attribute
in XSLT attributes.
The element of <xsl:attribute> creates an attribute in the output document, using any values that can be
accessed from the
Extensible Stylesheet Language. The element must be the first thing defined inside the output document element
for which it establishes attribute values.
Syntax for XSLT Attribute
<xsl:attribute name=NAME namespace=URI>
TEMPLATE
</xsl:attribute>
Complete Example of XSLT Attribute.
XML Code for XSLT Attribute Example
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="employees.xsl"?>
<employees>
<employee employee_id="032" employee_name="Michael R. Sweet" />
<employee employee_id="044" employee_name="Gerald Farin" />
<employee employee_id="168" employee_name="David Rogers" />
<employee employee_id="986" employee_name="Gerald Farin" />
</employees>
XSLT Code for XSLT Attribute Example
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head><title>Employees at Global Guide Line</title>
</head>
<body>
<h1>
<a href="https://www.globalguideline.com/xslt"><font color="0066CC">
Example of XSLT Attributes at Global Guide Line XSLT Tutorial
</font></a>
</h1>
<table width="100%" border="1">
<THEAD>
<TR>
<TD width="50%"><B>Employee ID</B></TD>
<TD width="50%"><B>Employee Name</B></TD>
</TR>
</THEAD>
<TBODY>
<xsl:for-each select="employees/employee">
<TR>
<TD width="50%"><xsl:value-of select="@employee_id" /></TD>
<TD width="50%"><xsl:value-of select="@employee_name" /></TD>
</TR>
</xsl:for-each>
</TBODY>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Details of XSLT Attribute
Attributes are referred to in the same way as elements are, but the attribute's name is prefixed by a @ symbol
as we used it on the above example..
<xsl:template match="/">
The example looks for the root element <employees>. The
transformation starts from there because this element is the node for the entire
Extensible Stylesheet Language document..
<xsl:for-each select="employees/employee">
A row is created for each <employee> element found inside the root
of <employees> in the above XML document.
<xsl:value-of select="@employee_id" /> <xsl:value-of select="@employee_name" />
The employee_id and author employee_name is taken from XML and displayed in a table
of HTML.
Output of XSLT Attribute Example with Sources
View complete sources and outputs by simply clicking on file name of XSLT or
Extensible Stylesheet Language
files.
Viewing XML Files in Firefox and Internet Explorer Browsers.
Open the XML file simply by double click - Then the XML document will be
displayed with color-coded root and child elements. Plus (+) or Minus mark (-) to the left of
the all elements can be clicked to expand or collapse the element structure. To view the raw XML source
of any XML document without the + and
- signs, select "View Page Source" or "View Source" from the browser menu of you
opened browser.
Viewing XML Files in Netscape Navigator.
Open the XML file in Netscape Navigator, then right-click in XML
document and select "View Page Source".
The XML file will then be displayed with color-coded root and child elements.
Viewing Files of XML in Opera 7.
Open the XML file in Opera browser, then right-click in XML
document and select "Frame" / "View Source".
The XML document will be displayed as plain text file with XML coiding..
Back
Next
|