
次のXSLスタイルシートは、HTML中の<a>タグを<b>タグに変換するサンプルです。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<!-- create tags for the document root -->
<xsl:template match="/">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<!-- Include this node and all descendant nodes and text unless otherwise excluded -->
<xsl:template match="/html[1]/head[1]
|/html[1]/head[1]//*
|/html[1]/head[1]//text()
|/html[1]/noframe[1]
|/html[1]/noframe[1]//*
|/html[1]/noframe[1]//text()
|/html[1]/frameset[1]
|/html[1]/frameset[1]//*
|/html[1]/frameset[1]//text()
|/html[1]/body[1]
|/html[1]/body[1]//*
|/html[1]/body[1]//text()">
<xsl:call-template name="passthru" />
</xsl:template>
<!-- template to translate 'a' tag to 'b' tag -->
<xsl:template match="a
|a//*
|a//text()" priority="1">
<xsl:element name="b">
<xsl:copy-of select="node()" />
</xsl:element>
</xsl:template>
<!-- default text template -->
<xsl:template priority="0" match="text()" />
<!-- template to duplicate the current node and apply templates -->
<xsl:template name="passthru">
<xsl:choose>
<xsl:when test="name()">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
|