Why doesn’t this line:
apply-templates select="*/* (under the “shop” element in the XSLT file)
apply bold formatting to Blake2? The output looks like this:
Start of root in XSLT
“Step 1 start” Alexis (Task: Sales )
Employee2 (Task: ) Blake2 “Step 1 done”End of root in XSLT
My question is, why isn’t Blake2 also in bold? It’s under the <employee> element.
Changing that line to *apply-templates select="*" causes Blake2 to be in bold. What makes this different?
Here’s the XML file:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="2.xsl" ?>
<root>
<shop>
<person>
<employee>
<name> Alexis </name>
<role> Manager </role>
<task> Sales </task>
</employee>
<employee>
<name> Employee2 </name>
</employee>
</person>
<employee>
<name> Blake2 </name>
</employee>
</shop>
</root>
Here’s the XSLT file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<html><head></head>
<body>
Start of root in XSLT <p/> <xsl:apply-templates /> <p/>
End of root in XSLT
</body>
</html>
</xsl:template>
<xsl:template match="shop">
"Step 1 start"
<xsl:apply-templates select="*/*"/>
"Step 1 done" <p/>
</xsl:template>
<xsl:template match="employee">
<b> <xsl:apply-templates select="name"/> </b>
(Task: <xsl:apply-templates select="task"/>)
<br></br>
</xsl:template>
</xsl:stylesheet>
Your third template does not select any
<employee>elements in your document. It just selects<employee>elements that are direct child nodes of the current element. On the root level, this does not apply to any elements, but as the second template is invoked by the<apply-templates>in the first element, and the second template applies templates with the (relative!) path*/*(i.e. templates for any elements that are children of the children of the current element), the<employee>element in the<person>element gets selected.The second
<employee>element is not a child of a child of the<shop>element, but a direct child of the<shop>element, and hence doesn’t get selected by*/*starting at<shop>.When you change
*/*to*, both<employee>elements get selected: The second one gets selected because it directly matches*(a direct child of<shop>).*also selects<person>, and as there is no template specified for that element, the default behavior will be executed, meaning that templates will be applied to the children of<person>, which includes the first<employee>element.