gopherprotocol Posted April 6, 2011 Share Posted April 6, 2011 I have an xml with a tree structure of:<Results> <Result> <Grade>Error</Grade> <MachinesFound>1</MachinesFound> <Machines> <Machine path="\\path">Machine</Machine> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Warning</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Error</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> <Machine path="\\path">Machine</Machine> </Machines> </Result> <Result> <Grade>Warning</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result>etc...</Results>I want to display in 3 colums the totals of Machines found for each grade. Is that possible? Link to comment Share on other sites More sharing options...
m.asfak Posted April 6, 2011 Share Posted April 6, 2011 I have an xml with a tree structure of:<Results> <Result> <Grade>Error</Grade> <MachinesFound>1</MachinesFound> <Machines> <Machine path="\\path">Machine</Machine> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Warning</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Error</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> <Machine path="\\path">Machine</Machine> </Machines> </Result> <Result> <Grade>Warning</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result>etc...</Results>I want to display in 3 colums the totals of Machines found for each grade. Is that possible?The simplest one.<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> <xsl:output indent="yes" /> <xsl:template match="/"> <html> <head></head> <body> <table> <tr> <td>Error</td> <td>Critical</td> <td>Warning</td> </tr> <tr> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Error'])"/></td> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Critical'])"/></td> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Warning'])"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Best regards. Link to comment Share on other sites More sharing options...
Martin Honnen Posted April 6, 2011 Share Posted April 6, 2011 There is not much to compute in the sample you posted as most values are 0 but the following is an XSLT 2.0 stylesheet (that can be run with XSLT 2.0 processors like Saxon 9, XQSharp, AltovaXML): <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="html" indent="yes"/> <xsl:template match="Results"> <xsl:variable name="groups"> <xsl:for-each-group select="Result" group-by="Grade"> <group> <xsl:copy-of select="current-group()"/> </group> </xsl:for-each-group> </xsl:variable> <table> <thead> <tr> <xsl:for-each select="$groups/group"> <th> <xsl:value-of select="Result[1]/Grade"/> </th> </xsl:for-each> </tr> </thead> <tbody> <tr> <xsl:for-each select="$groups/group"> <td> <xsl:value-of select="sum(Result/MachinesFound)"/> </td> </xsl:for-each> </tr> </tbody> </table> </xsl:template> </xsl:stylesheet> When applied with Saxon 9 to the input <Results> <Result> <Grade>Error</Grade> <MachinesFound>1</MachinesFound> <Machines> <Machine path="\\path">Machine</Machine> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines></Machines> </Result> <Result> <Grade>Warning</Grade> <MachinesFound>0</MachinesFound> <Machines></Machines> </Result> <Result> <Grade>Error</Grade> <MachinesFound>0</MachinesFound> <Machines></Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> <Machine path="\\path">Machine</Machine> </Machines> </Result> <Result> <Grade>Warning</Grade> <MachinesFound>0</MachinesFound> <Machines></Machines> </Result></Results> the output is as follows: <table> <thead> <tr> <th>Error</th> <th>Critical</th> <th>Warning</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>0</td> <td>0</td> </tr> </tbody></table> Let us know whether that helps or if you need an XSLT 1.0 solution (Muenchian grouping with XSLT 1.0 is described here: http://www.jenitennison.com/xslt/grouping/muenchian.xml). Link to comment Share on other sites More sharing options...
gopherprotocol Posted April 6, 2011 Author Share Posted April 6, 2011 The simplest one.<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> <xsl:output indent="yes" /> <xsl:template match="/"> <html> <head></head> <body> <table> <tr> <td>Error</td> <td>Critical</td> <td>Warning</td> </tr> <tr> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Error'])"/></td> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Critical'])"/></td> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Warning'])"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Best regards. Worked perfect. Link to comment Share on other sites More sharing options...
gopherprotocol Posted April 8, 2011 Author Share Posted April 8, 2011 The simplest one.<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> <xsl:output indent="yes" /> <xsl:template match="/"> <html> <head></head> <body> <table> <tr> <td>Error</td> <td>Critical</td> <td>Warning</td> </tr> <tr> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Error'])"/></td> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Critical'])"/></td> <td><xsl:value-of select="count(//Machine[normalize-space(.)!='' and ancestor::Result/Grade='Warning'])"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Best regards. This worked great but i want to combine some xml to one so i created another element in my xml<Scans> <Tier> <Results> <Check>Somthing here.</Check> <Result> <Grade>Error</Grade> <MachinesFound>1</MachinesFound> <Machines> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Check>BLAHBLAHBLAH.</Check> <Result> <Grade>Error</Grade> <MachinesFound>5</MachinesFound> <Machines> <Machine path="C:\HA">HMMM</Machine> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>3</MachinesFound> <Machines> </Machines> </Result> </Results> </Tier> <Tier> <Results> <Check>MORE JUNK.</Check> <Result> <Grade>Not Approved</Grade> <MachinesFound>1</MachinesFound> <Machines> <Machine path="C:\theway">Othercomputer</Machine> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>0</MachinesFound> <Machines> </Machines> </Result> <Check>Bingo.</Check> <Result> <Grade>Warning</Grade> <MachinesFound>5</MachinesFound> <Machines> <Machine path="c:\thepath">Computer</Machine> </Machines> </Result> <Result> <Grade>Critical</Grade> <MachinesFound>3</MachinesFound> <Machines> </Machines> </Result> </Results> </Tier></Scans> Now if i can i want a table like above for each tier. Is that possible? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.