HTML and CSS Reference
In-Depth Information
</xsl:template>
</xsl:stylesheet>
Listing 5.17 modifies this slightly to add id attributes to only p , table , and div elements. You can easily adjust
the list of elements you add id s to by changing the element names in the match pattern in the second template
rule.
Listing 5.17. An XSLT Stylesheet That Adds IDs to Particular Elements
Code View:
<?xml version='1.0'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:html='http://www.w3.org/1999/xhtml'>
<!-- match elements that already have IDs -->
<xsl:template match='html:*[@id]' priority='1.5'>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match elements we will add IDs to -->
<xsl:template match='html:p | html:table | html:div'
priority='1.25'>
<xsl:copy>
<xsl:attribute name='id'>
<xsl:value-of select='generate-id()'/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match elements we're not going to add IDs to -->
<xsl:template match='*' priority='1'>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match non-elements -->
<xsl:template match='@*|node()' priority='0.5'>
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Search WWH ::




Custom Search