HTML and CSS Reference
In-Depth Information
You simply add an id attribute to each start-tag, like so:
<p id='p1'>Game over. Sony has
<a id='a1' href=
and Blu-Ray has lost.</p>
There are a few rules for the values of id attributes, but they're not especially onerous.
The value must begin with a letter.
The value must contain only letters and digits.
Each value must be unique within the document where it appears.
The ID values do not have to have any particular system or meaning, though they can if that's convenient for
you. Usually, however, they're pretty much arbitrary strings.
Assuming the documents are already well-formed, it's easy enough to automate this process. Listing 5.16
demonstrates a simple XSLT stylesheet that adds IDs to all elements in a document that don't already have
Listing 5.16. An XSLT Stylesheet That Adds IDs to All Elements
Code View:
<xsl:stylesheet xmlns:xsl=''
<!-- match elements with IDs -->
<xsl:template match='*[@id]' priority='1.5'>
<xsl:apply-templates select="@*|node()"/>
<!-- match elements without IDs -->
<xsl:template match='*' priority='1'>
<xsl:attribute name='id'>
<xsl:value-of select='generate-id()'/>
<xsl:apply-templates select="@*|node()"/>
<!-- match non-elements -->
<xsl:template match='@*|node()' priority='0.5'>
<xsl:apply-templates select="node()"/>
Search WWH ::

Custom Search