[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Conditionally Sum an Attribute


Subject: [xsl] Conditionally Sum an Attribute
From: "Ronnie Royston" <rhroyston@xxxxxxxxx>
Date: Sat, 31 May 2008 19:03:44 -0500

Using Saxon-B 9.0 on Windows.  I am really trying and could use a
nudge (or shove) in the right direction.  I posted on this problem
before and have grown the problem.  I'm running a stylesheet against a
well-formed braindump of my knowledge on each type of thing that I
sell during my day job (routers, switches, etc.) and am using the
document('mySkus.xml') function to determine what parts of my
knowledge.xml to output to "Scope of Work.html".  The idea is that a
custom scope of work is generated based on what I am selling (what is
in mySkus.xml).  Snippets of both XML files are at the bottom of this
email.

I can't figure out how to concatenate the sum of all mySku's
@quantities that share a starts-with <name> from my knowledge, with
their <name> from my knowledge to generate the desired output.  For
example, I want to output "DataVox is offering Big Momma's Cafe
project services at a fixed price for the installation of 1 router, 3
switches, and 7 phones."

Here's my unsuccessful stylesheet.  The problem lies in the variable
names "names".  Please understand this is the last attempt - I was
probably close on one of the many attempts I've made (lol).  One last
thing - don't worry about the "s" on plural items - I'll save that
battle for another day:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
 version="2.0" >
<xsl:param name="customer">Customer</xsl:param>
<xsl:key name="sku" match="knowledge/part" use="code" />

<xsl:variable name="root" select="/"/>
<xsl:variable name="skus" as="xs:string*"
select="distinct-values(/knowledge/part/code)" />

<xsl:variable name="names" select="for $s in $skus return concat(
sum(key('sku',$s[document('mySkus.xml')/skus/sku[starts-with(.,$s)]],@quantity),
key('sku',$s[document('mySkus.xml')/skus/sku[starts-with(.,$s)]],$root)/name)"
/>

<xsl:template match="/">

<xsl:result-document href="file:///C:\Saxon\bin\proposal.html">
 <html>
 <head>
  <title>Project Services from DataVox</title>
 </head>
 <body>
  <H1>Executive Summary</H1>
<H2>Project Services Summary</H2>
<H3>Introduction</H3>
<p>
DataVox is offering <xsl:value-of select="$customer"/> project
services at a fixed price for the installation of
<xsl:for-each select="$names">
<xsl:choose>
	<xsl:when test="position()=1">
		<xsl:value-of select="concat(' ', .)"/>
	</xsl:when>
	<xsl:when test="position()=last()">
		<xsl:value-of select="concat(', and ', ., '. ')"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="concat(', ', .)"/>
	</xsl:otherwise>
</xsl:choose>
</xsl:for-each>

</p>

 </body>
 </html>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>


mySkus.xml:
<?xml version="1.0"?>
<skus>
<sku quantity="3">WS-C3750G-24PS-S</sku>
<sku quantity="7">CP-7937G</sku>
<sku quantity="10">CP-7941G</sku>
<sku quantity="5">WS-X6708-10G-3C</sku>
<sku quantity="2">CISCO2811-V/K9</sku>
</skus>

knowledge.xml
<?xml version="1.0"?>
<knowledge>
	<part>
		<code>CP-</code>
		<name>Cisco phones</name>
		<abbreviation>handsets</abbreviation>
		<discovery>call forwarding destinations</discovery>
		<verify>Out-dialing and In-dialing to a single phone in each
configured class of service ensures a working dialplan and handset
configuration.  </verify>
		<responsibility>unbox, assemble, and place phones at their final
locations</responsibility>
	</part>
	<part>
		<code>ATA186-</code>
		<name>Cisco Analog Telephone Adapters</name>
		<abbreviation>ATA</abbreviation>
		<discovery>directory number assignments</discovery>
		<verify>In-dialing to a fax machine is tested, if applicable.  </verify>
	</part>
	<part>
		<code>CISCO</code>
		<name>Cisco routers</name>
		<abbreviation>routers</abbreviation>
		<discovery>routing protocols to be enabled</discovery>
		<verify>ping an IP address on a local interface</verify>
	</part>
</knowledge>


Current Thread
Keywords
xml