473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Finding Zero, 1 or more items in XML via XSL.

<Restaurants>
<FastFood>
<DriveIn Name="Del Taco"/>
<DriveIn Name="McDonalds"/>
<DriveIn Name="Bakers"/>
<DriveIn Name="Arbys"/>
<DriveIn Name="Green Burrito"/>
<DriveIn Name="Jack in the Box"/>
<DriveIn Name="Green Burrito"/>
<DriveIn Name="KFC"/>
<DriveIn Name="Burger King"/>
<DriveIn Name="El Pollo Loco"/>
</FastFood>
</Restaurants>

I need to know how to build an XSL file that will:

1) give me a count of how many DriveIn elements have the
word "Green" in the Name attribute.

2) output the Name attributes that do have "Green" in them, in a
comma-separated list, with a period after the last one.

Green Drive Ins: Green Burrito, Green Burrito.

the data may have zero occurences...

<Restaurants>
<FastFood>
<DriveIn Name="Del Taco"/>
<DriveIn Name="McDonalds"/>
<DriveIn Name="Bakers"/>
<DriveIn Name="Arbys"/>
<DriveIn Name="Jack in the Box"/>
<DriveIn Name="KFC"/>
<DriveIn Name="Burger King"/>
<DriveIn Name="El Pollo Loco"/>
</FastFood>
</Restaurants>

3) When the XML has zero DriveIn elements with "Green" in the Name
attribute, it'll output like:

Green Drive Ins: None.

I've been able to list the names like

Green Drive Ins: Green Burrito, Green Burrito,

but not determine when to place a period and not a comma on the last
one, or how to place an alternate string, indicating none were listed.

Any help will be greatly appreciated. I'm using MS XML 4, SP2.
Jul 20 '05 #1
3 1570
Maybe someone else will post a complete solution, but here are pointers to
your specific problems

"Gadrin77" <ga*****@aol.com> wrote in message
news:ff**************************@posting.google.c om...
<Restaurants>
<FastFood>
<DriveIn Name="Del Taco"/>
<DriveIn Name="McDonalds"/>
<DriveIn Name="Bakers"/>
<DriveIn Name="Arbys"/>
<DriveIn Name="Green Burrito"/>
<DriveIn Name="Jack in the Box"/>
<DriveIn Name="Green Burrito"/>
<DriveIn Name="KFC"/>
<DriveIn Name="Burger King"/>
<DriveIn Name="El Pollo Loco"/>
</FastFood>
</Restaurants>

I need to know how to build an XSL file that will:

1) give me a count of how many DriveIn elements have the
word "Green" in the Name attribute.

2) output the Name attributes that do have "Green" in them, in a
comma-separated list, with a period after the last one.

Green Drive Ins: Green Burrito, Green Burrito.

the data may have zero occurences...

<Restaurants>
<FastFood>
<DriveIn Name="Del Taco"/>
<DriveIn Name="McDonalds"/>
<DriveIn Name="Bakers"/>
<DriveIn Name="Arbys"/>
<DriveIn Name="Jack in the Box"/>
<DriveIn Name="KFC"/>
<DriveIn Name="Burger King"/>
<DriveIn Name="El Pollo Loco"/>
</FastFood>
</Restaurants>
to get the comment, you will need something like
<xsl:if test="position() <> last()">,</xsl:if>

3) When the XML has zero DriveIn elements with "Green" in the Name
attribute, it'll output like:

Green Drive Ins: None.

<xsl:choose>
<xsl:when test="count(...) = 0">
None
</xsl:when>
<xsl:otherwise>
<xsl: apply-templates select="..."/> (or whatever)
</xsl:otherwise>
</xsl:otherwise>

I've been able to list the names like

Green Drive Ins: Green Burrito, Green Burrito,

but not determine when to place a period and not a comma on the last
one, or how to place an alternate string, indicating none were listed.

Any help will be greatly appreciated. I'm using MS XML 4, SP2.

Jul 20 '05 #2
Gadrin77 wrote:
<Restaurants>
<FastFood>
<DriveIn Name="Del Taco"/>
<DriveIn Name="McDonalds"/>
<DriveIn Name="Bakers"/>
<DriveIn Name="Arbys"/>
<DriveIn Name="Green Burrito"/>
<DriveIn Name="Jack in the Box"/>
<DriveIn Name="Green Burrito"/>
<DriveIn Name="KFC"/>
<DriveIn Name="Burger King"/>
<DriveIn Name="El Pollo Loco"/>
</FastFood>
</Restaurants>

I need to know how to build an XSL file that will:

1) give me a count of how many DriveIn elements have the
word "Green" in the Name attribute.

2) output the Name attributes that do have "Green" in them, in a
comma-separated list, with a period after the last one.

Green Drive Ins: Green Burrito, Green Burrito.

the data may have zero occurences...

<Restaurants>
<FastFood>
<DriveIn Name="Del Taco"/>
<DriveIn Name="McDonalds"/>
<DriveIn Name="Bakers"/>
<DriveIn Name="Arbys"/>
<DriveIn Name="Jack in the Box"/>
<DriveIn Name="KFC"/>
<DriveIn Name="Burger King"/>
<DriveIn Name="El Pollo Loco"/>
</FastFood>
</Restaurants>

3) When the XML has zero DriveIn elements with "Green" in the Name
attribute, it'll output like:

Green Drive Ins: None.

I've been able to list the names like

Green Drive Ins: Green Burrito, Green Burrito,

but not determine when to place a period and not a comma on the last
one, or how to place an alternate string, indicating none were listed.

Any help will be greatly appreciated. I'm using MS XML 4, SP2.


Here is an example stylesheet, it outputs HTML but of course you can
make it output text as well:

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

<xsl:template match="/">
<xsl:variable name="GreenDriveIns"
select="/Restaurants/FastFood/DriveIn[contains(@Name, 'Green')]" />
<html>
<head>
<title>DriveIns</title>
</head>
<body>
<div>
<p>There are <xsl:value-of select="count($GreenDriveIns)" />
green drive ins.</p>
<p>
Green Drive Ins:
<xsl:choose>
<xsl:when test="not($GreenDriveIns)">
<xsl:text>none.</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$GreenDriveIns">
<xsl:value-of select="@Name" />
<xsl:choose>
<xsl:when test="position() = last()">
<xsl:text>.</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>, </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</p>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #3
Martin Honnen <ma*******@yahoo.de> wrote in message news:<40***********************@newsread2.arcor-online.net>...

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

<xsl:template match="/">
<xsl:variable name="GreenDriveIns"
select="/Restaurants/FastFood/DriveIn[contains(@Name, 'Green')]" />
<html>
<head>
<title>DriveIns</title>
</head>
<body>
<div>
<p>There are <xsl:value-of select="count($GreenDriveIns)" />
green drive ins.</p>

Thanks Martin! The use of the xsl:variable and the count($variable) is
very interesting.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: cwsullivan | last post by:
Hi gang, I have a grid full of particles in my program, and I want to find an angle between two particles. I'm having trouble because it seems like the answer depends on whether or not the target...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
17
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question...
22
by: Simon Forman | last post by:
Is there a more efficient way to do this? def f(L): '''Return a set of the items that occur more than once in L.''' L = list(L) for item in set(L): L.remove(item) return set(L)
4
by: jameswilkinsonfjs | last post by:
Hi All, Ok I have a table - it lists items with a unique reference code; lets say there are 4 items : Item RefCode 1 ABC1 2 ABC2 3 ...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.