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=
"http://www.sgknox.com/2007/01/11/no-porn-on-blu-ray/">
forfeited</a>
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
them.
Listing 5.16. An XSLT Stylesheet That Adds IDs to All Elements
Code View:
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>
<!-- match elements with IDs -->
<xsl:template match='*[@id]' priority='1.5'>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match elements without IDs -->
<xsl:template match='*' priority='1'>
<xsl:copy>
<xsl:attribute name='id'>
<xsl:value-of select='generate-id()'/>
</xsl:attribute>
<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>
Search WWH ::




Custom Search