Elements and Attributes

Contact Us or call 1-877-932-8228
Elements and Attributes

Elements and Attributes

When outputting XML or HTML, you will need to output tags and attributes. We saw earlier that you can output tags using literal result elements.

xsl:element

The xsl:element tag can be used to explicitly specify that an element is for output. You will notice that the xsl:element tag takes the name attribute, which is used to specify the name of the element being output.

Also, as shown, xsl:element tags can be nested within each other.

Code Sample:

XsltBasics/Demos/Name2.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" version="1.0"/>
	<xsl:template match="name">
		<xsl:element name="Matches">
			<xsl:element name="Match">We found a name!</xsl:element>
			<xsl:element name="Name">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>

When XsltBasics/Demos/Name2.xml, which has the same XML as XsltBasics/Demos/Name.xml , is transformed against XsltBasics/Demos/Name2.xsl, the output looks the same as we saw when we used literal result elements earlier in the lesson.

The xsl:element tag is most useful when the tag name itself is generated. If the tag name is not generated, it is generally easier to use literal result elements.

xsl:attribute

The xsl:attribute element is similar to the xsl:element element. It is used to explicitly output an attribute. However, it is more commonly used than xsl:element. To see why, let's first look at an example in which an attribute is output literally.

Code Sample:

XsltBasics/Demos/NameLREwithAtt.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" version="1.0"/>
	<xsl:template match="name">
		<Matches>
			<Match Name="Some Name">We found a name!</Match>
		</Matches>
	</xsl:template>
</xsl:stylesheet>

Notice that the value of the Name attribute is just "Some Name". We would like to generate this value from the source XML document by doing something like this:

<!--THIS IS POORLY FORMED-->
<Match Name="<xsl:value-of select='.'/>">
	We found a name!
</Match>

Code Sample:

XsltBasics/Demos/NameWithAtt.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" version="1.0"/>
	<xsl:template match="name">
		<Matches>
			<Match>
				<xsl:attribute name="Name">
					<xsl:value-of select="firstname"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="lastname"/>
				</xsl:attribute>We found a name!</Match>
		</Matches>
	</xsl:template>
</xsl:stylesheet>

The xsl:attribute tag applies to the element that it is nested within; in this case, the Match element. When XsltBasics/Demos/NameWithAtt.xml, which has the same XML as XSLTBasics/Demos/Name.xml , is transformed against XsltBasics/Demos/NameWithAtt.xsl, the output looks like this:

Code Sample:

XsltBasics/Demos/NameWithAttResult.xml
<?xml version="1.0" encoding="UTF-8"?>
<Matches>
	<Match Name="Paul McCartney">We found a name!</Match>
</Matches>

Attributes and Curly Brackets

Because using xsl:attribute can be a bit laborious, an abbreviated syntax is available. An XPath within curly brackets ({}) can be embedded as the value of an attribute in the open tag. The following example illustrates this.

Code Sample:

XsltBasics/Demos/NameWithAttAbbr.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" version="1.0"/>
	<xsl:template match="name">
		<Matches>
			<Match Name="{firstname} {lastname}">We found a name!</Match>
		</Matches>
	</xsl:template>
</xsl:stylesheet>

This will have the exact same result as XsltBasics/Demos/NameWithAtt.xsl , but it is much quicker and easier to write. To see the result, transform XsltBasics/Demos/NameWithAttAbbr.xml against this XSLT.

Next