Re: [xsl] Generating XSLT

Subject: Re: [xsl] Generating XSLT
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 12 Nov 2004 15:54:31 -0500
Hi James,

It'd be way better, instead of for-eaching over the entire set of $tags/tagsDecl/tagUsage, you simply applied templates to them.

Then you could have a template for any kind of tag you expect (match="tagUsage[@gi='div']", etc), plus one to catch any tags you didn't expect.

It's a perfect example of what Dimitre and Evan were talking about in the thread earlier this week, that starts at

http://www.biglist.com/lists/xsl-list/archives/200411/msg00499.html

Cheers,
Wendell

At 12:11 PM 11/12/2004, you wrote:
Hiya,

I tried to knock up a quick xslt script which would take any script as
its input and generate a very basic xslt stylesheet template.  I.e.
produce something quickly which I could then
edit to make more specific.  Since I already had a script which did an
element count, I stuck that into a variable, and then use the content
in that variable to produce a template for each element name.  (Ok, I
know that wasn't the most efficient way, just what occured to me first
when I threw it together).  Having done that, what is the best way to
include extra rules for specific elements it may or may not come
across.  I've done this by adding <xsl:choose>
for the specific instances.  Better suggestions?
-------------------
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="gis" match="*" use="name()"/>

<xsl:variable name="tags">
<xsl:for-each select="node()">
<tagsDecl>
<xsl:for-each select="//*[generate-id(.)=generate-id(key('gis',name(.))[1])]">
 <xsl:sort select="name()"/>
 <tagUsage gi="{name(.)}"><xsl:value-of select="count(key('gis',
name(.)))" /></tagUsage>
</xsl:for-each>
</tagsDecl>
</xsl:for-each>
</xsl:variable>

<xsl:template match="/">
<xsl:element name="xsl:stylesheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:attribute name="version" select="'2.0'"/>
<xsl:element name="xsl:template">
<xsl:attribute name="match" select="'/'"/>
<html> <!-- ok, I'd make this xhtml 1.1 in reality -->
<head><xsl:if test="//title"><title><xsl:value-of
select="//title[1]"/></title></xsl:if></head>
<body>
<xsl:if test="//title"><h1><xsl:value-of select="//title[1]"/></h1></xsl:if>
<xsl:element name="xsl:apply-templates" />
</body>
</html>
</xsl:element>
<xsl:for-each select="$tags/tagsDecl/tagUsage">
<xsl:choose>
<xsl:when test="@gi='div'">
<xsl:element name="xsl:template">
<xsl:attribute name="match" select="@gi"/>
<div class="{@gi}"><xsl:element name="xsl:apply-templates" /> </div>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="xsl:template">
<xsl:attribute name="match" select="@gi"/>
<span class="{@gi}"><xsl:element name="xsl:apply-templates" /></span>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
----------------------------

--
James Cummings, Cummings dot James at GMail dot com


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread