Link to home
Start Free TrialLog in
Avatar of baxleyb
baxleyb

asked on

XSL file with multiple conditions (see description)

Hello,

I have 4 XSL files that I am using to display data from one XML file.

I would like to use a single XSL file that applies different attributes based on specific conditions found in the XML file.

The conditions should be defined in the XSL file.

Below is an example of an XSL file that is checking for a condition in the XML file:

Any assistance would be greatly appreciated!
<xsl:if test="contains(mainName:stuff,'homes')"> <--- Condition check here
      <div class="date"><xsl:value-of select="mainName:shortDate"/></div>
      <div class="headline">
		  <xsl:element name="a">
			<xsl:attribute name="href">
				<xsl:value-of select="link"/>
			</xsl:attribute>
			<xsl:attribute name="title">
				<xsl:value-of select="description"/>
			</xsl:attribute>
			<xsl:value-of select="title"/>
		  </xsl:element>
	  </div>
	  </xsl:if>
 
Right now I am using 4 xsl files to pull from one XML file, I would like to consolidate this into one xsl file, kind of like this:
 
<xsl:if test="contains(mainName:stuff,'homes')">
      <div class="date"><xsl:value-of select="mainName:shortDate"/></div>
      <div class="headline">
		  <xsl:element name="a">
			<xsl:attribute name="href">
				<xsl:value-of select="link"/>
			</xsl:attribute>
			<xsl:attribute name="title">
				<xsl:value-of select="description"/>
			</xsl:attribute>
			<xsl:value-of select="title"/>
		  </xsl:element>
	  </div>
	  </xsl:if>
 
<xsl:if test="contains(mainName:other,'cars')">
      <div class="date"><xsl:value-of select="mainName:shortDate"/></div>
      <div class="headline">
		  <xsl:element name="a">
			<xsl:attribute name="href">
				<xsl:value-of select="link"/>
			</xsl:attribute>
			<xsl:attribute name="title">
				<xsl:value-of select="description"/>
			</xsl:attribute>
			<xsl:value-of select="title"/>
		  </xsl:element>
	  </div>
	  </xsl:if>

Open in new window

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

multiple conditions can be chosen from using a choose construct
<xsl:choose>
<xsl:when test="contains(mainName:stuff,'homes')">
...
</xsl:when>
<xsl:when test="contains(mainName:stuff,'cars')">
...
</xsl:when>
</xsl:choose>

the when clause of the first test that results in a true, will get active
compared to a series of if statements: each true if test results in an active if
you can put the choose statemnet at a different level too

eg.
    <div class="headline">
        <xsl:element name="a">
            <xsl:attribute name="href">
                <xsl:value-of select="link"/>
            </xsl:attribute>
            <xsl:choose>
                <xsl:when test="contains(mainName:stuff,'homes')">
                    <xsl:attribute name="hometitle">
                        <xsl:value-of select="description"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="contains(mainName:stuff,'cars')">
                    <xsl:attribute name="cartitle">
                        <xsl:value-of select="description"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <xsl:value-of select="title"/>
        </xsl:element>
    </div>
Avatar of baxleyb
baxleyb

ASKER

Hello and thanks for your responses.

I've already tried this but the problem is that I only want specific nodes to display on specific pages when the conditions are met.


For example:

This code should run on the "homes" page when this condition is met:

<xsl:when test="contains(mainName:stuff,'homes')">
                    <xsl:attribute name="hometitle">
                        <xsl:value-of select="description"/>
                    </xsl:attribute>
                </xsl:when>

This code should run on the "cars" page when this condition is met:

  <xsl:when test="contains(mainName:stuff,'cars')">
                    <xsl:attribute name="cartitle">
                        <xsl:value-of select="description"/>
                    </xsl:attribute>
                </xsl:when>

Also please keep in mind that the namespaces must remain in tact:

e.g. <xsl:value-of select="title"/> vs. <xsl:value-of select="cartitle"/>

if this is not possible then please let me know.

Thanks for your assistance
I used "cartitle" as an example that something else is possible

anyway, I don't understand your question,
can you be more specific of what you want to do, maybe give XML examples and LOTS of explanation,
because this is really not clear
Avatar of baxleyb

ASKER

Hi Gertrone,

Thanks for your help thus far.

I have added the XML file that is being used.

Right now using the below XML file with 4 different XSL files works fine.

I have 4 separate asp web pages that display the XML data using the

XSL, this code is added to the asp pages:

'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("my.xml"))

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath(str_XSL))

'Transform file
Response.Write(xml.transformNode(xsl))

For example if the homepage is being viewed then only the data for the following 2 namespaces should be displayed:
=====================================================================
<item>
                  <title>Idaho Utility Regulators Approve Somebody Clean Coal Big Plant</title>
                  <link>http://www.mysite.com/topics/releases/2007112001.asp</link>
                  <description>The Utility Regulatory Commission granted Somebody permission today to construct a technologically advanced clean coal power plant in Edwardsport, Ind.</description>
                  <pubDate>Wed, 20 Nov 2007 04:00:00 UTC</pubDate>
                  <mainName:shortDate>11/20/2007</mainName:shortDate>
                  <mainName:homePage>yes</mainName:homePage> <--- This is the variable that is checked to display on the homepage
                  <mainName:topics>environment investors</mainName:topics>
                  <alink>http://www.mysite.com/topics/releases/2007112001.asp</alink>
</item>

<item>
                  <title>Somebody Names Two New Directors; Declares Big Payout</title>
                  <link>http://www.mysite.com/topics/releases/2007102502.asp</link>
                  <description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
                  <pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
                  <mainName:shortDate>10/25/2007</mainName:shortDate>
                  <mainName:homePage>yes</mainName:homePage> <--- This is the variable that is checked to display on the homepage
                  <mainName:topics>investors</mainName:topics>
                  <alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
            </item>
=====================================================================

So I would like to basically switch XSL templates based on a specific conditions:

For example:

<xsl:when test="mainName:homePage='yes'">

      display homepagexsl.xsl

<xsl:when test="contains(mainName:stuff,'homes')">

      display homesxsl.xsl

Does this make more sense now? Is this possible?

Thanks in advance for your response.
<channel>
		<title>Top Stories</title>
		<link>http://www.mysite.com</link>
		<description>Somebody Corporation</description>
		<language>en-us</language>
		<pubDate>Mon, 12 Nov 2007 18:48:20 UTC</pubDate>
		<docs>http://www.mysite.com/rss</docs>
		<generator>Somebody</generator>
		<item>
			<title>Idaho Utility Regulators Approve Somebody Clean Coal Big Plant</title>
			<link>http://www.mysite.com/topics/releases/2007112001.asp</link>
			<description>The Utility Regulatory Commission granted Somebody permission today to construct a technologically advanced clean coal power plant in Edwardsport, Ind.</description>
			<pubDate>Wed, 20 Nov 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>11/20/2007</mainName:shortDate>
			<mainName:homePage>yes</mainName:homePage>
			<mainName:topics>environment investors</mainName:topics>
			<alink>http://www.mysite.com/topics/releases/2007112001.asp</alink>
		</item>
		<item>
			<title>Big Costs Would Dramatically Increase under Lieberman-Warner Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007111501.asp</link>
			<description>According to an analysis by Somebody, the plan proposed by U.S. Sens. Frick and Frack to require an auction for a large portion of carbon emissions allowances would hit customers hard in the 25 states that generate more than half their electricity from coal.</description>
			<pubDate>Wed, 15 Nov 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>11/15/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<mainName:topics>environment</mainName:topics>
			<alink>http://www.mysite.com/topics/releases/2007111501.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>yes</mainName:homePage>
			<mainName:topics>investors</mainName:topics>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
		<item>
			<title>Somebody Idaho Issues Call for Renewable Energy </title>
			<link>http://www.mysite.com/topics/releases/2007102402.asp</link>
			<description>Somebody Idaho has issued a  Request for Proposals  seeking bids for power generated from renewable energy sources, including sun, wind, water, organic matter and other sources. </description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<mainName:topics>environment</mainName:topics>
			<alink>http://www.mysite.com/topics/releases/2007102402.asp</alink>
		</item>
		<item>
			<title>Somebody Comments on Proposed Frick/Frack Climate Change Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007102401.asp</link>
			<description>Somebody today issued the following statement regarding climate change legislation proposed by U.S. Sens. Frick and Frack on Oct. 18, 2007. The legislation will be considered by the Senate Environment and Public Works Committee in a hearing today.</description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<mainName:topics>environment</mainName:topics>
			<alink>http://www.mysite.com/topics/releases/2007102401.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout-NO</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
		<item>
			<title>Somebody Idaho Issues Call for Renewable Energy </title>
			<link>http://www.mysite.com/topics/releases/2007102402.asp</link>
			<description>Somebody Idaho has issued a  Request for Proposals  seeking bids for power generated from renewable energy sources, including sun, wind, water, organic matter and other sources. </description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102402.asp</alink>
		</item>
		<item>
			<title>Somebody Comments on Proposed Frick/Frack Climate Change Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007102401.asp</link>
			<description>Somebody today issued the following statement regarding climate change legislation proposed by U.S. Sens. Frick and Frack on Oct. 18, 2007. The legislation will be considered by the Senate Environment and Public Works Committee in a hearing today.</description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102401.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
		<item>
			<title>Somebody Idaho Issues Call for Renewable Energy </title>
			<link>http://www.mysite.com/topics/releases/2007102402.asp</link>
			<description>Somebody Idaho has issued a  Request for Proposals  seeking bids for power generated from renewable energy sources, including sun, wind, water, organic matter and other sources. </description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102402.asp</alink>
		</item>
		<item>
			<title>Somebody Comments on Proposed Frick/Frack Climate Change Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007102401.asp</link>
			<description>Somebody today issued the following statement regarding climate change legislation proposed by U.S. Sens. Frick and Frack on Oct. 18, 2007. The legislation will be considered by the Senate Environment and Public Works Committee in a hearing today.</description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102401.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
		<item>
			<title>Somebody Idaho Issues Call for Renewable Energy </title>
			<link>http://www.mysite.com/topics/releases/2007102402.asp</link>
			<description>Somebody Idaho has issued a  Request for Proposals  seeking bids for power generated from renewable energy sources, including sun, wind, water, organic matter and other sources. </description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102402.asp</alink>
		</item>
		<item>
			<title>Somebody Comments on Proposed Frick/Frack Climate Change Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007102401.asp</link>
			<description>Somebody today issued the following statement regarding climate change legislation proposed by U.S. Sens. Frick and Frack on Oct. 18, 2007. The legislation will be considered by the Senate Environment and Public Works Committee in a hearing today.</description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102401.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
		<item>
			<title>Somebody Idaho Issues Call for Renewable Energy </title>
			<link>http://www.mysite.com/topics/releases/2007102402.asp</link>
			<description>Somebody Idaho has issued a  Request for Proposals  seeking bids for power generated from renewable energy sources, including sun, wind, water, organic matter and other sources. </description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102402.asp</alink>
		</item>
		<item>
			<title>Somebody Comments on Proposed Frick/Frack Climate Change Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007102401.asp</link>
			<description>Somebody today issued the following statement regarding climate change legislation proposed by U.S. Sens. Frick and Frack on Oct. 18, 2007. The legislation will be considered by the Senate Environment and Public Works Committee in a hearing today.</description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102401.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
		<item>
			<title>Somebody Idaho Issues Call for Renewable Energy </title>
			<link>http://www.mysite.com/topics/releases/2007102402.asp</link>
			<description>Somebody Idaho has issued a  Request for Proposals  seeking bids for power generated from renewable energy sources, including sun, wind, water, organic matter and other sources. </description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102402.asp</alink>
		</item>
		<item>
			<title>Somebody Comments on Proposed Frick/Frack Climate Change Legislation</title>
			<link>http://www.mysite.com/topics/releases/2007102401.asp</link>
			<description>Somebody today issued the following statement regarding climate change legislation proposed by U.S. Sens. Frick and Frack on Oct. 18, 2007. The legislation will be considered by the Senate Environment and Public Works Committee in a hearing today.</description>
			<pubDate>Wed, 24 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/24/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102401.asp</alink>
		</item>
		<item>
			<title>Somebody Names Two New Directors; Declares Big Payout</title>
			<link>http://www.mysite.com/topics/releases/2007102502.asp</link>
			<description>Somebody announced that its board of directors increased its size from 10 to 12 members with the election today of Bonnie and Clyde.</description>
			<pubDate>Thu, 25 Oct 2007 04:00:00 UTC</pubDate>
			<mainName:shortDate>10/25/2007</mainName:shortDate>
			<mainName:homePage>no</mainName:homePage>
			<alink>http://www.mysite.com/topics/releases/2007102502.asp</alink>
		</item>
	</channel>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Valliappan AN
Valliappan AN
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial