|
-
June 10th, 2002, 04:27 AM
#1
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!
-
June 11th, 2002, 01:04 AM
#2
Hi
<xsl:template match="someNode">
<xsl aram name="parName"/>
........
<xsl:value-of select="$parName"/>
.......
</xsl:template>
inside another template
....
<xsl:apply-templates select="//someNode">
<xsl:with-param name="parName">whatever you want, for example-<xsl:value-of select="someXPath"/></xsl:with-param>
</xsl:apply-templates>
....
Good luck
-
June 11th, 2002, 02:25 AM
#3
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 aram
is there the solution for xsl:variable?
-
June 11th, 2002, 06:01 AM
#4
Sorry I did not catch in the begining what do you really need.
Try this sample. I think you have expected this:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="x.xsl"?>
<PLANETS>
<xref code="R" colorName="RED"/>
<PLANET COLOR="R">
<NAME>Mercury</NAME>
<MASS UNITS="(Earth = 1)">.0553</MASS>
<DAY UNITS="days">58.65</DAY>
</PLANET>
</PLANETS>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<xsl:apply-templates select="//PLANETS/PLANET"/>
</HTML>
</xsl:template>
<xsl:template match="PLANET">
<xsl:variable name="var" select="@COLOR"/>
<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of select="//xref[@code=$var]/@colorName"/>
</h2>
</xsl:template>
</xsl:stylesheet>
Good luck
-
June 11th, 2002, 10:17 PM
#5
thanks a lot
I modified your code and it works as the results here:
the Mercury is RED
the Venus is RED
thanks a lot for your help. Think that the xsl:variable is not be able to use like
<xsl:template match="PLANET[@COLOR='$RED_COLOR']">
<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of select="$RED_COLOR"/>
</h2>
</xsl:templte>
so that the xsl:if is not need to be used. and the <xref>in the xml is not needed.
by the way use PLANET[@COLOR='$RED_COLOR'] is not a error, just the match is not correct. maybe xsl treats the $RED_COLOR as a whole string not a string value of a variable
thanks again
the xsl code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<xsl:apply-templates select="//PLANETS/PLANET"/>
</HTML>
</xsl:template>
<xsl:template match="PLANET">
<xsl:variable name="var" select="//PLANETS/PLANET/@COLOR"/>
<xsl:variable name="var2" select="//PLANETS/xref/@linkend"/>
<xsl:if test="$var=$var2">
<h2>the <xsl:value-of select="NAME"/> is <xsl:value-of select="//xref[@linkend=$var]/@linkend"/>
</h2>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
xml code
<?xml version="1.0"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by zou xiong (tempcompany) -->
<?xml-stylesheet type="text/xsl" href="E:\XML-XSL-XSLT\planets.xsl"?>
<PLANETS>
<PLANET COLOR="RED">
<NAME>Mercury</NAME>
<MASS UNITS="(Earth = 1)">.0553</MASS>
<DAY UNITS="days">58.65</DAY>
<RADIUS UNITS="miles">1516</RADIUS>
<DENSITY UNITS="(Earth = 1)">.983</DENSITY>
<DISTANCE UNITS="million miles">43.4</DISTANCE>
<!--At perihelion-->
</PLANET>
<PLANET COLOR="RED">
<NAME>Venus</NAME>
<MASS UNITS="(Earth = 1)">.815</MASS>
<DAY UNITS="days">116.75</DAY>
<RADIUS UNITS="miles">3716</RADIUS>
<DENSITY UNITS="(Earth = 1)">.943</DENSITY>
<DISTANCE UNITS="million miles">66.8</DISTANCE>
<!--At perihelion-->
</PLANET>
<PLANET COLOR="BLUE">
<NAME>Earth</NAME>
<MASS UNITS="(Earth = 1)">1</MASS>
<DAY UNITS="days">1</DAY>
<RADIUS UNITS="miles">2107</RADIUS>
<DENSITY UNITS="(Earth = 1)">1</DENSITY>
<DISTANCE UNITS="million miles">128.4</DISTANCE>
<!--At perihelion-->
</PLANET>
<xref linkend="RED"/>
</PLANETS>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|