Reputation: 115
I have this (quite odd) XML, with 7 children from 4 families. There are 5 boys that have 6 apples and 9 oranges and 2 girls that have 3 apples and 3 oranges.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fruits.xsl"?>
<report>
<family>
<gender>
<boyorgirl>Boy</boyorgirl>
<person>
<apples>1</apples>
<oranges>1</oranges>
<id>1</id>
</person>
</gender>
<gender>
<boyorgirl>Girl</boyorgirl>
<person>
<apples>2</apples>
<oranges>0</oranges>
<id>2</id>
</person>
</gender>
<gender>
<boyorgirl>Boy</boyorgirl>
<person>
<apples>1</apples>
<oranges>4</oranges>
<id>3</id>
</person>
</gender>
</family>
<family>
<gender>
<boyorgirl>Girl</boyorgirl>
<person>
<apples>1</apples>
<oranges>3</oranges>
<id>4</id>
</person>
</gender>
</family>
<family>
<gender>
<boyorgirl>Boy</boyorgirl>
<person>
<apples>1</apples>
<oranges>0</oranges>
<id>5</id>
</person>
</gender>
</family>
<family>
<gender>
<boyorgirl>Boy</boyorgirl>
<person>
<apples>2</apples>
<oranges>2</oranges>
<id>6</id>
</person>
<person>
<apples>1</apples>
<oranges>2</oranges>
<id>7</id>
</person>
</gender>
</family>
</report>
I want to count how many persons there are for each gender, and how many apples and oranges they have together. I don't want to hardcode on gender in those modern days, in case someone should claim they have another gender than boy or girl. Expected output is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fruitcounting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fruitline>
<boyorgirl>Boy</boyorgirl>
<numberOfPersons>5</numberOfPersons>
<apples>6</apples>
<oranges>9</oranges>
</fruitline>
<fruitline>
<boyorgirl>Girl</boyorgirl>
<numberOfPersons>2</numberOfPersons>
<apples>3</apples>
<oranges>3</oranges>
</fruitline>
</fruitcounting>
I have the beginning of a stylesheet, but it just counts members and fruits in the first family - how can I change it so it finds all boys/girls, irrespective of family? Or, I think I could formulate it as all nodes that have the key as preceding sibling.
I can only use XSL 1.0 without extensions
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="fruits" match="gender" use="boyorgirl"/>
<xsl:template match="/">
<fruitcounting>
<xsl:for-each select="//gender[generate-id(.)=generate-id(key('fruits', boyorgirl)[1])]">
<fruitline>
<boyorgirl><xsl:value-of select="boyorgirl"/></boyorgirl>
<numberOfPersons><xsl:value-of select="count(../gender/person/id)"/></numberOfPersons>
<apples><xsl:value-of select="sum(../gender/person/apples)"/></apples>
<oranges><xsl:value-of select="sum(../gender/person/oranges)"/></oranges>
</fruitline>
</xsl:for-each>
</fruitcounting>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 82
Reputation: 167716
You can replace <numberOfPersons><xsl:value-of select="count(../gender/person/id)"/></numberOfPersons>
with <numberOfPersons><xsl:value-of select="count(key('fruits', boyorgirl)/person)"/></numberOfPersons>
and use the key
function the same way to idenfity your groups, e.g. <apples><xsl:value-of select="sum(key('fruits', boyorgirl)/person/apples)"/></apples>
in the other computations.
So the complete code becomes
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="fruits" match="gender" use="boyorgirl"/>
<xsl:template match="/">
<fruitcounting>
<xsl:for-each select="//gender[generate-id(.)=generate-id(key('fruits', boyorgirl)[1])]">
<fruitline>
<boyorgirl><xsl:value-of select="boyorgirl"/></boyorgirl>
<numberOfPersons><xsl:value-of select="count(key('fruits', boyorgirl)/person)"/></numberOfPersons>
<apples><xsl:value-of select="sum(key('fruits', boyorgirl)/person/apples)"/></apples>
<oranges><xsl:value-of select="sum(key('fruits', boyorgirl)/person/oranges)"/></oranges>
</fruitline>
</xsl:for-each>
</fruitcounting>
</xsl:template>
</xsl:stylesheet>
online at http://xsltransform.net/bwdwsb.
Upvotes: 1