I'm trying to sum the qty field in my xml using xslt 2.0 and it's outputting the qty of one of my pieces of equipment with multiple entries without summing by the equipment group. Any ideas about how this can be fixed?
This is the XSLT I'm using:
<xsl:for-each-group select="//node()" group-by="*[local-name()='lin_id']/text()">
<Results>
<xsl:element name="LIN_ID"><xsl:value-of select="*[local-name()='lin_id']"/></xsl:element>
<xsl:element name="Count"><xsl:value-of select="sum(current-group()/*[local-name()='qty'])"/></xsl:element>
</Results>
</xsl:for-each-group>
Here's the source XML file:
<Equipment>
<lin_id>C18312</lin_id>
<qty>5</qty>
</Equipment>
<Equipment>
<lin_id>C18345</lin_id>
<qty>22</qty>
</Equipment>
<Equipment>
<lin_id>C18378</lin_id>
<qty>43</qty>
</Equipment>
<Equipment>
<lin_id>C18378</lin_id>
<qty>208</qty>
</Equipment>
开发者_如何学运维
And here's what the output is currently:
<Results>
<LIN_ID>C18312</LIN_Name>
<Count>5</Count>
</Results>
<Results>
<LIN_ID>C18345</LIN_Name>
<Count>22</Count>
</Results>
<Results>
<LIN_ID>C18378</LIN_Name>
<Count>43</Count>
</Results>
So you can see that it's doing the grouping but for LIN_ID C18378 it should be summing the qty of the 2 entries and outputting a count of 251 but it instead it's just displaying one of the values.
First off - what's <xsl:for-each-group select="//node()" >
supposed to do? Try:
<xsl:for-each-group select="//Equipment" group-by="lin_id">
<Results>
<LIN_ID>
<xsl:value-of select="current-grouping-key()"/>
</LIN_ID>
<Count>
<xsl:value-of select="sum(current-group()/qty)"/>
</Count>
</Results>
</xsl:for-each-group>
If all that *[local-name()='…']
business is because of namespaces, I suggest you get your namespace declarations straight. Because this is unnecessary and borderline horrible. ;-)
Also: There is no need to write <xsl:element name="LIN_ID">
, you can just write <LIN_ID>
directly, as shown above.
For debugging purposes, you could do:
<Count qty_nodes="{count(current-group()/qty)}">
<xsl:value-of select="sum(current-group()/qty)"/>
</Count>
to see how many nodes current-group()/qty
returns.
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<top>
<xsl:for-each-group select="Equipment/lin_id" group-by=".">
<Results>
<LIN_ID>
<xsl:sequence select="current-grouping-key()"/>
</LIN_ID>
<Count>
<xsl:sequence select="sum(current-group()/../qty/xs:integer(.))"/>
</Count>
</Results>
</xsl:for-each-group>
</top>
</xsl:template>
</xsl:stylesheet>
when applied against the provided input (corrected to be a wellformed XML file):
<t>
<Equipment>
<lin_id>C18312</lin_id>
<qty>5</qty>
</Equipment>
<Equipment>
<lin_id>C18345</lin_id>
<qty>22</qty>
</Equipment>
<Equipment>
<lin_id>C18378</lin_id>
<qty>43</qty>
</Equipment>
<Equipment>
<lin_id>C18378</lin_id>
<qty>208</qty>
</Equipment>
</t>
produces the wanted result:
<top>
<Results>
<LIN_ID>C18312</LIN_ID>
<Count>5</Count>
</Results>
<Results>
<LIN_ID>C18345</LIN_ID>
<Count>22</Count>
</Results>
<Results>
<LIN_ID>C18378</LIN_ID>
<Count>251</Count>
</Results>
</top>
精彩评论