[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

XSL file with multiple conditions (see description)

Posted on 2007-12-04
6
Medium Priority
?
4,628 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:baxleyb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20405385
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
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20405421
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>
0
 

Author Comment

by:baxleyb
ID: 20406117
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20406421
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
0
 

Author Comment

by:baxleyb
ID: 20412242
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

0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 525 total points
ID: 20519040
You have 2  options : In both options, you need to give unique template names for each function provided by the 4 xslt files (use xsl:template ):

1) Maintain 4 xslt files and put your choose condition in a 5th common xslt file (if you want that sort of readability), like this:

<!-- common.xslt -- goes like this - you can use yous namespace here -->

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="file1.xsl"/>
<xsl:import href="file2.xsl"/>
<xsl:import href="file3.xsl"/>
<xsl:import href="file4.xsl"/>

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

      <xsl:call-template name = "file1template"/>

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

      <xsl:call-template name = "file2template"/>

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

      <xsl:call-template name = "file3template"/>

</xsl:choose>

</xsl:stylesheet>

2) Put all xslt file templates in same file and call the <xslt:choose> as above, the only difference is that you dont use imports.

Hope that helps.
Thanks.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question