Solved

XSL file with multiple conditions (see description)

Posted on 2007-12-04
6
4,603 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
  • 3
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
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 175 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now