I am starting doing with xml and xslt, I also read some tutorials etc.. and according these as I understand this xml:
<?xml version="1.0"?>
<Menu>
<Accounts type="menu" label="Accounts">
<ListUsers type="cmd" label="List users">
<cmd>HAha</cmd>
</ListUsers>
<AddUsers type="cmd" label="Add users">
<cmd></cmd>
</AddUsers>
<DeleteUsers type="cmd" label="Delete users">
<cmd></cmd>
</DeleteUsers>
</Accounts>
<Hardware type="menu" label="Hardware">
<ListDisks type="cmd" label="List disks">
<cmd></cmd>
</ListDisks>
</Hardware>
<Network type="menu" label="Network"></Network>
<Filesystem type="menu" label="Filesystem"></Filesystem>
</Menu>
and this xslt:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<div><xsl:value-of select="@label"/></div>
</xsl:template>
</xsl:stylesheet>
Should output labels of all elements, but it didn’t… What I am missing, could you correct and explain me please, thanks.
There are two problems with this code:
Elements that aren’t children of the top element (or the top element itself) are not processed at all. To correct this, a new instruction should be appended to the body of the template — for example:
<xsl:apply-templates select="*"/>Elements that don’t have a
labelattribute will generate an empty<div>. This can be avoided if a proper template match pattern is used so that the template is selected only for elements that do have this attribute.Here is a complete solution and it can be as short and simple as:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced: