xsl-list
[Top] [All Lists]

Re: [xsl] The identity transform and all <a> links: xslt 1.0 - nearly there!!

2008-01-24 09:12:41
On 24/01/2008, Marroc <marrocdanderfluff(_at_)yahoo(_dot_)co(_dot_)in> wrote:
I now present the completed working transform for the benefit of future list
users. This transform uses the identity transform  to transfer everything
from XHTML to XHTML, replete with doctype and namespace declarations, while
converting the links to a new format. Notice particularly how the default
namespace is set in the third line of the stylesheet element:

You don't actually need to do that... see below.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

xmlns:xhtml="http://www.w3.org/1999/xhtml";

xmlns="http://www.w3.org/1999/xhtml";

exclude-result-prefixes="xhtml">

        <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"

doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
                        indent="yes" encoding="UTF-8" method="xml"/>


        <xsl:template match="node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
        </xsl:template>

        <xsl:template match="@*">
                <xsl:copy/>
        </xsl:template>

        <xsl:template match="xhtml:a">
                <xsl:element name="a">
                        <xsl:attribute
name="href">javascript:include('<xsl:value-of
select="@href"/>?i=1','BODY');</xsl:attribute>
                </xsl:element>
        </xsl:template>
</xsl:stylesheet>

One problem with this is that you aren't copying across any existing
attributes on the xhtml:a element (such as @class), so you might be
better off restricting the match to just the href attribute:

<xsl:template match="xhtml:a/@href">
  <xsl:attribute name="href">
    <xsl:text>javascript:include('</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>?i=1','BODY');</xsl:text>
  </xsl:attribute>
</xsl:template>

Doing that also removes the need to set the default namespace, as
you're not creating any new elements, just copying them across.


cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--