Extract list and count of different values
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 80
- Joined: Wed Jan 14, 2009 12:50 pm
Extract list and count of different values
I have data of the following form:
<a>
<b att="x"> ... </b>
<b att="c"> ... </b>
<b att="y"> ... </b>
<b att="z"> ... </b>
<b att="x"> ... </b>
<b att="x"> ... </b>
<b att="z"> ... </b>
<b att="c"> ... </b>
</a>
I want to generate a table based on this data in the following format, a list of all the values used for att paired with the number of b elements that have each att. The values for att are not restricted by the schema.
I can see how to do this when I know what the possible values of att are using count(descendant:b[att='c']) and so on. But I don't know how to dynamically build a list of each of the different att values used. As usual, I'm sure this is easy to do and I'm just not seeing the obvious solution. Any help will be appreciated.
<a>
<b att="x"> ... </b>
<b att="c"> ... </b>
<b att="y"> ... </b>
<b att="z"> ... </b>
<b att="x"> ... </b>
<b att="x"> ... </b>
<b att="z"> ... </b>
<b att="c"> ... </b>
</a>
I want to generate a table based on this data in the following format, a list of all the values used for att paired with the number of b elements that have each att. The values for att are not restricted by the schema.
Code: Select all
att number of elements
c 2
x 3
y 1
z 2
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Extract list and count of different values
This is very easy with XSLT 2.0:
Best Regards,
George
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="a">
<xsl:text>att		number of elements </xsl:text>
<xsl:for-each-group select="b" group-by="@att">
<xsl:value-of select="current-grouping-key()"/>
<xsl:text>		</xsl:text>
<xsl:value-of select="count(current-group())"/>
<xsl:text> </xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
George
George Cristian Bina
-
- Posts: 80
- Joined: Wed Jan 14, 2009 12:50 pm
Re: Extract list and count of different values
Brilliant. Thanks.
Can I use for-each-group to produce groups of the actual elements with interspersed material like group headings? e.g.
<a>
<c>Type C</c>
<b att="c"> ... </b>
<b att="c"> ... </b>
<c>Type X</c>
<b att="x"> ... </b>
<b att="x"> ... </b>
<b att="x"> ... </b>
<c>Type Y</c>
<b att="y"> ... </b>
<c>Type Z</c>
<b att="z"> ... </b>
<b att="z"> ... </b>
</a>
where the <b> elements are also then sorted within their groups by their content.
Can I use for-each-group to produce groups of the actual elements with interspersed material like group headings? e.g.
<a>
<c>Type C</c>
<b att="c"> ... </b>
<b att="c"> ... </b>
<c>Type X</c>
<b att="x"> ... </b>
<b att="x"> ... </b>
<b att="x"> ... </b>
<c>Type Y</c>
<b att="y"> ... </b>
<c>Type Z</c>
<b att="z"> ... </b>
<b att="z"> ... </b>
</a>
where the <b> elements are also then sorted within their groups by their content.
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Extract list and count of different values
Sure
Best Regards,
George
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="a">
<a>
<xsl:for-each-group select="b" group-by="@att">
<c type="{current-grouping-key()}"/>
<xsl:for-each select="current-group()">
<xsl:sort select="."/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each-group>
</a>
</xsl:template>
</xsl:stylesheet>
George
George Cristian Bina
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ Artificial Intelligence (AI Positron Assistant add-on)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service