<table-group>
<html>
<head><title></title></head>
<body>
<table width="885">
<tr>
<td width="20%"><p></p></td>
<td width="20%"><p></p></td>
<td width="60%"><p></p></td>
</tr>
<tr>
<th align="left" valign="bottom"><p><b>Revision Date</b></p></th>
<th align="left" valign="bottom"><p><b>Sections Revised</b></p></th>
<th align="left" valign="bottom"><p><b>Description</b></p></th>
</tr>
<tr>
<td align="left" valign="top"><p>7/1/02</p></td>
<td align="left" valign="top"><p>All</p></td>
<td align="left" valign="top"><p>Complete manual revision to reflect changes related to the MMIS and HIPAA compliance.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>7/1/02</p></td>
<td align="left" valign="top"><p>8.0</p></td>
<td align="left" valign="top"><p>Local code YY604 was mapped to HCPCS code G9012 incorrectly in the manual as S9012. The code and definition were corrected.</p></td>
</tr>
</table>

*************my xslt file is here but no give the sum of column but work all function of my file.

<xsl:template match="table">
<h3 align="center">TABLE WIDTH:<xsl:value-of select="@width"/></h3>
<table>
<tr>
<xsl:for-each select="tr/td">
<td><xsl:value-of select="@width"/></td>
</xsl:for-each>
</tr>
<xsl:for-each select="tr">
<tr>
<xsl:choose>
<xsl:when test="th">
<xsl:for-each select="th">
<th style="background-color:skyblue;"><xsl:copy-of select="@*|node()"/></th>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="td">
<td style="background-color:orange"><xsl:copy-of select="@*|node()"/></td>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

Recommended Answers

All 7 Replies

Are you adding the sum of the widths? If so, the code does not have any addition functions in it. Could you clarify a bit?

ya i want adding sum of widths. But how i use the adding function i do not know.Please advice me
<td width="20%"><p></p></td>
<td width="20%"><p></p></td>
<td width="60%"><p></p></td>
Output==
20+20+60=100

You will probably have to use javascript to store each width attribute as a variable and then add it together. I don't believe that XSLT contains addition functions...I'm pretty sure that's not what you wanted to hear, but I think if nothing else it would be simplest solution. If you just need to test if one value is larger than another, I think it's possible...

Do you have to do this using XSLT/XPath?

This is easily done in XSLT, don't let above poster tell you otherwise. There's a large library of functions available in XSLT and XPATH that can do this.

In XSLT 1.0, it proves to fairly annoying and somewhat complicated. For all the examples of XSLT I've written, I stripped down your input document to the following:

<table-group>
	<html>
		<head>
			<title></title>
		</head>
		<body>
			<table width="885">
				<tr>
					<td width="15%" />
					<td width="25%"/>
					<td width="60%"/>
				</tr>
			</table>
		</body>
	</html>
</table-group>

In XSLT 1.0, you first need to take your width and remove the "%" sign from each one. You cannot quickly add string with a % sign after them in 1.0. Once you've removed them, you have to temporarily store the resulting values ('15,'25,'60') in a tree and them sum across those temporary nodes. Lastly, if you want the "%" sign back on the end you concat the value returned with a "%" sign. This 1.0 solution does this on the above input document.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:template match="/">
		<xsl:apply-templates select="//table/tr[td[@width]]" mode="sum"/>
	</xsl:template>
		
	<xsl:template match="//table/tr[td[@width]]" mode="sum">
		<xsl:variable name="widthlist">
			<widths>
				<xsl:for-each select="td/@width">
					<width>
						<xsl:value-of select="replace(.,'%','')"/>
					</width>
				</xsl:for-each>
			</widths>
		</xsl:variable>		
		<WidthSum>
			<xsl:value-of select="concat(sum($widthlist/widths/width),'%')" />
		</WidthSum>
	</xsl:template>
</xsl:stylesheet>

In XSLT 2.0, this is a MUCH easier task. You can directly replace the "%" sign and sum them all in one statement without the need to store a temporary tree of values. This makes for a long but actually very simple value-of expression.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:template match="/">
		<xsl:apply-templates select="//table/tr[td[@width]]"/>
	</xsl:template>

	<xsl:template match="tr[td[@width]]">
		<WidthSum>
			<xsl:value-of select="concat(sum(td/number(substring-before(@width,'%'))),'%')"/>
		</WidthSum>
	</xsl:template>
</xsl:stylesheet>

The output of both of the XSLTs is as follows:

<WidthSum>100%</WidthSum>

Hopefully you can use these to do what you want to do. Unfortunately, because you're in the web world, chances are you're going to have to use the 1.0 solution.

Good luck!

hi
when i use your fisrt code it gives the output.
'replace' is not a valid XSLT or XPath function. -->replace(.,'%','')<--
in the output.what are doing please advice me.
thanks &regareds
Raghavendra

hi
when i use your second code for the view of my xml file.
it gives output

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
NodeTest expected here. concat(sum(td/-->number<--(substring-before(@width,'%'))),'%')

what are doing please advice me.
thanks &regareds
Raghavendra

If you're having problem with replace(), use translate() instead.

I already stated that the second solution is an XSLT 2.0 solution. That's the error when you run a 2.0 solution on a 1.0 processor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.