RE: [xsl] Design question

Subject: RE: [xsl] Design question
From: "XSLList" <xsllist@xxxxxxxxxxxxx>
Date: Sun, 9 Feb 2003 20:53:04 -0500
Wanted:
>BEFORE:
>     <test type="positive" name="Test 1">
>         <param name="p1">123</param>
>         <param name="date1">July 9</param>
>         <param name="p2">false</param>
>     </test>
>
>AFTER:
>     <da:Positive name="Test 1" p1="123" p2="false">
>        <da:Date number="1" value="July 9"/>
>     </da:Positive>
>

I tested this and it works.  As I see it, the weakness in my approach is
that I'm making assumptions in how your param elements may vary.  (I.e. I
hard-coded 'date' in one template, and make the assumption that the names
won't vary.)  It was tested using MSXML 4.  It seems to me that you could
easily build templates for each name pattern and extend this to be more
robust.

I'm just learning, so these little exercises are helpful!  --Jeff



<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
	<xsl:apply-templates select="test"/>
</xsl:template>

<xsl:template match="test">
	<xsl:element name="{@type}">
		<xsl:copy-of select="@name"/>
		<xsl:apply-templates select="param[contains(@name,'p')]"/>
		<xsl:apply-templates select="param[contains(@name,'date')]"/>
	</xsl:element>
</xsl:template>

<xsl:template match="param[contains(@name,'p')]">
	<xsl:attribute name="{@name}">
		<xsl:value-of select="."/>
	</xsl:attribute>
</xsl:template>

<xsl:template match="param[contains(@name,'date')]">
		<xsl:element name="Date">
			<xsl:attribute name="number">
				<xsl:value-of select="translate(@name,
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz','')"/>
			</xsl:attribute>

			<xsl:attribute name="value">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
</xsl:template>
</xsl:stylesheet>



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread