Formatting using XSLT works by selecting an element in the XML document then choosing an appropriate template. A template specifies the transformation that is to be applied to that specific element and its content.
This XML file was used for all the examples.
|
<xsl:template match="path"> <xsl:apply-templates/> |
The "match" attribute of the <xsl:template> element is specified to declare to which elements the templates will apply. If a forward slash is stated as its value, the path the xslt processor will follow is from the Top Level Element and down. This element is always stated with an opening and closing tag. <xsl:template> </xsl:template>
The <xsl:apply-templates/> element is stated within the <body> of the <html> document. This allows the content of ALL the XML elements to be displayed in HTML format conducive to browser rendering. This element is always stated as an empty element.
Example 1
This example states <xsl:template match="Employee">. An absolute path.
View XSL stylesheet
View result
Example 2
This example states <xsl:template match="/">. This path uses an Expression. A relative path.
View XSL stylesheet
View result
Example 3
This example states <xsl:template match="/Employee">. This states to the processor to look for all ParentElements of the Employee element. This path uses an Expression. A relative path.
View XSL stylesheet
View result
Example 4
This example states <xsl:template match="/PhoneExt">. This states to the processor to look for all the PhoneExt elements of ANY ParentElement. This path uses an Expression. A relative path.
View XSL stylesheet
View result
Example 5
This example states that the formatting of the contents of the xml document will be contained within an XHTML <table> element along with CSS formatting.
View XSL stylesheet
View result
|
<apply-templates select="path"/> |
The <apply-templates/> element when used with the select attribute is more specific about exactly what elements from the XML document will be given the styling.
Example 1
This example states <xsl:template match="Employee"> and <xsl:apply-templates select="PhoneExt"/>.
These paths state: "Look for the Employee element, then find the direct ChildElement, PhoneExt, and apply the formatting to the PhoneExt element.
View the XSL stylesheet
View result
Example 2A
This example states <xsl:template match="Employee"> and <xsl:apply-templates select="Employee/PhoneExt"/>.
These paths state: "Look for the Employee element, then find the direct ChildElement, Employee, and apply the formatting to the PhoneExt element.
A display will NOT be rendered because there is NOT ANOTHER another ChildElement named Employee within the XML document.
View the XSL stylesheet
View result
Example 2B
This example states <xsl:template match="Employee"> and <xsl:apply-templates select="Employee/PhoneExt"/>.
These paths state: "Look for the Employee element, then find the direct ChildElement, Employee, and apply the formatting to the PhoneExt element.
A display WILL be rendered because there IS ANOTHER another ChildElement named Employee within the XML document.
The XML document has been altered to state another Employee element as a direct ChildElement of the ParentElement, Employee.
View the altered XML document
View the XSL stylesheet
View result
Example 3
This example states <xsl:template match="/"> and <xsl:apply-templates select="Employee/PhoneExt"/>.
View the XSL stylesheet
View result
Examples 4
When an absolute path is stated as the value for the match attribute, it causes the rendering of ALL the content of the XML document, but the formatting is applied only to the contents of the last stated element.
In both example 4A and 4B, PhoneExt will be the content to which the formatting will be applied becasue it is the last stated element.
Since the <xsl:apply-templates/> is not specific as to exactly what elements should be displayed for the resulting HTML document because it DOES NOT contain the select attribute, the processor defaults to the rendering of ALL the XML document's content as stated by the path of the match attribute in the <xsl:template> element.
Use the select attribute for the <xsl:apply-templates/> element to be more specific about the selection of which elements are to be DISPLAYED with the stated formatting.
Example 4A
This example states <xsl:template match="Employee/PhoneExt"> and <xsl:apply-templates/>.
View the XSL stylesheet
View result
Example 4B
This example states <xsl:template match="/Employee/PhoneExt"> and <xsl:apply-templates/>.
View the XSL stylesheet
View result
Example 5
This example states <xsl:template match="/"> and <xsl:apply-templates select="PhoneExt"/>.
The processor DOES NOT render a display because the select attribute carries a path value of PhoneExt. The processor is unable to locate PhoneExt because it is NOT the top level element of the XML document.
View the XSL stylesheet
View result
Example 6
This example states <xsl:template match="/Employee"> and <xsl:apply-templates select="PhoneExt"/>.
The processor WILL render a display because the match attribute carries a path value of Employee.
The top level element of the XML document MUST be specified when a relative path is stated for the select attribute's value.
View the XSL stylesheet
View result |