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

G

Gadrin77

<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.
 
A

Andy Fish

Maybe someone else will post a complete solution, but here are pointers to
your specific problems

Gadrin77 said:
<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
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:eek:therwise>
<xsl: apply-templates select="..."/> (or whatever)
</xsl:eek:therwise>
 
M

Martin Honnen

Gadrin77 said:
<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:eek:therwise>
<xsl:for-each select="$GreenDriveIns">
<xsl:value-of select="@Name" />
<xsl:choose>
<xsl:when test="position() = last()">
<xsl:text>.</xsl:text>
</xsl:when>
<xsl:eek:therwise>
<xsl:text>, </xsl:text>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
</xsl:eek:therwise>
</xsl:choose>
</p>
</div>
</body>
</html>
</xsl:template>


</xsl:stylesheet>
 
G

Gadrin77

<?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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top