xml-xalan-j-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joanne Tong <joan...@ca.ibm.com>
Subject Re: XSL newbie - way to do dynamic variables?
Date Mon, 03 May 2004 15:48:30 GMT
Here are 2 stylesheets that will produce the same result.

Hope this helps.

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

<xsl:template match="/">
        <xsl:call-template name="doThis">
                 <xsl:with-param name="pageBody" select="product"/>
                 <xsl:with-param name="field1" select="'name'"/>
                 <xsl:with-param name="field2" select="'color'"/>  
        </xsl:call-template>
</xsl:template>

<xsl:template name="doThis">
        <xsl:param name="pageBody"/>
        <xsl:param name="field1"/>
        <xsl:param name="field2"/>
        <xsl:value-of select="$field1"/> : <xsl:value-of select=
"$pageBody/*[name()=$field1]"/>
        <xsl:value-of select="$field2"/> : <xsl:value-of select=
"$pageBody/*[name()=$field2]"/> 
</xsl:template>

</xsl:stylesheet>

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

<xsl:template match="/">
        <xsl:call-template name="doThis">
                 <xsl:with-param name="pageBody" select="product"/>
        </xsl:call-template>
</xsl:template>

<xsl:template name="doThis">
        <xsl:param name="pageBody"/>
        <xsl:for-each select="$pageBody/*">
                <xsl:value-of select="name(.)"/> : <xsl:value-of 
select="."/>
        </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Joanne




"Brian" <bryhardt@yahoo.com> 
05/03/2004 11:21 AM

To
<xalan-j-users@xml.apache.org>
cc

Subject
XSL newbie - way to do dynamic variables?






given this simple XML

-----------------------------------------------

<product>
                 <color>red</color>
                 <name>car</name>
</product>

-----------------------------------------------

i want to call a XSL template and pass the tag name as dynamic variable - 
so
the temlate being called can process a variety of incoming XML and give 
the
same result HTML

-----------------------------------------------

<xsl:call-template name="doThis">
                 <xsl:with-param name="pageBody" select="product"/>
                 <xsl:with-param name="field1" select="name"/>
                 <xsl:with-param name="field2" select="color"/>
</xsl:call-template>

-----------------------------------------------

so that if dynamic vars would work (showing it like a php $$) it would 
look
something like this (of course this doesnt work)...

-----------------------------------------------

<xsl:param name="pageBody"/>
<xsl:param name="field1"/>
<xsl:param name="field2"/>

<xsl:value-of select="$field1"/> : <xsl:value-of select="$$field1"/>
<xsl:value-of select="$field2"/> : <xsl:value-of select="$$field2"/>

-----------------------------------------------

so the output would be something like
name : car
color : red

i could even live with reformatting the input XML and placing it into a
variable in the format that the calling template would expect - but i
couldnt get the variable/parameter to parse as XML into the calling 
template

any help?

thanks,
Brian...



Mime
View raw message