xsl:variable with attribute
Hi,
I want to use the xsl:varriable in the <xsl:template match="">
the following is what I tried with <xsl:apply-templates select="">
the xsl file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="PLANETS">
<HTML>
<xsl:apply-templates/>
</HTML>
</xsl:template>
<xsl:template match="//PLANETS/PLANET">
<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of
select="$pcolor"/></h2>
</xsl:template>
<xsl:variable name="pcolor"/>
<xsl:template match="xref">
<xsl:variable name="pcolor" select="@linkend"/>
<xsl:apply-templates select="//PLANETS/*[@COLOR=$pcolor]"/>
</xsl:template>
<xsl:template match="text()">
</xsl:template>
</xsl:stylesheet>
the xml file:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="planets.xsl"?><PLANETS>
<xref linkend="RED"/>
<PLANET COLOR="RED">
<NAME>Mercury</NAME>
<MASS UNITS="(Earth = 1)">.0553</MASS>
<DAY UNITS="days">58.65</DAY>
</PLANET>
</PLANETS>
I explicitly declare the xsl:variable pcolor at the global scope too otherwise the xsl will report error with pcolor not declared before useing it in
<xsl:template match="//PLANETS/PLANET">
<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of
select="$pcolor"/></h2>
</xsl:template>
the problem with the previous example is that the variable pcolor can not be assigned with value RED and passed to "<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of select="$pcolor"/></h2>". if I can define a variable pcolor equal to RED and use
<xsl:template match="//PLANETS/PLANET[@COLOR='{$pcolor}']">
<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of
select="$pcolor"/></h2>
</xsl:template>
there is no above problems.
Anybody can give the solutions?
Thanks in advance!
Best Regards!
thanks but it doesnot work
I am using the xml spy to do the conversion. the way you suggested didnot work in my pc. Can you give me the entire xsl? and you suggested is to use xsl:param
is there the solution for xsl:variable?