Parsing and counting using XSLT
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 4
- Joined: Wed Jan 23, 2008 2:01 pm
Parsing and counting using XSLT
Post by manish_jaiswal »
Hello!
I m trying to transform a XML file into new XML file .
Description:
Each unique value from the <name> elements in the input record are to be uniqued, alphabetized and stored with a "count" attribute. The new "count" attribute provides the number of occurrences of that value .Must parse and input element on semi-colon if this delimits a list of values.
input XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<School>
<student rec='1'>
<name>Robart J</name>
</student>
<student rec='2'>
<name>Alex M; Peter R</name>
</student>
<student rec='3'>
<name>Alex M;Robart J; Dave T</name>
</student>
</School>
Output XML should be like..
<School>
<name count='2'>Robart J</name>
<name count='2'>Alex M</name>
<name count='1'>Peter R</name>
<name count='1'>Dave T</name>
</School>
I am using XSLT 2.0 processor. I just can't work it out. Is this even possible with XSL? Or is there a
better way of doing the substitution that I'm missing? Please let me know if it is possible in XSLT 1.0 ….
Thanks in advance
Manish
I m trying to transform a XML file into new XML file .
Description:
Each unique value from the <name> elements in the input record are to be uniqued, alphabetized and stored with a "count" attribute. The new "count" attribute provides the number of occurrences of that value .Must parse and input element on semi-colon if this delimits a list of values.
input XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<School>
<student rec='1'>
<name>Robart J</name>
</student>
<student rec='2'>
<name>Alex M; Peter R</name>
</student>
<student rec='3'>
<name>Alex M;Robart J; Dave T</name>
</student>
</School>
Output XML should be like..
<School>
<name count='2'>Robart J</name>
<name count='2'>Alex M</name>
<name count='1'>Peter R</name>
<name count='1'>Dave T</name>
</School>
I am using XSLT 2.0 processor. I just can't work it out. Is this even possible with XSL? Or is there a
better way of doing the substitution that I'm missing? Please let me know if it is possible in XSLT 1.0 ….
Thanks in advance
Manish
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Post by sorin_ristache »
Hello,
It seems a transformation that can be done in XSLT 1.0. You should study an XSLT tutorial for learning how you can transform the input XML file to the output XML file.
Regards,
Sorin
It seems a transformation that can be done in XSLT 1.0. You should study an XSLT tutorial for learning how you can transform the input XML file to the output XML file.
Regards,
Sorin
-
- Posts: 89
- Joined: Mon Mar 06, 2006 10:13 pm
I would suggest creating a temporary result for each name, with each name parsed out. eg temporary result would be:
<name>Robart J</name>
<name>Alex M</name>
<name>Peter R</name>
<name>Alex M</name>
<name>Robart J</name>
<name>Dave T</name>
Then use the count() function to do it.
It would be easier in XSLT 2.0 if you use this method.
<name>Robart J</name>
<name>Alex M</name>
<name>Peter R</name>
<name>Alex M</name>
<name>Robart J</name>
<name>Dave T</name>
Then use the count() function to do it.
It would be easier in XSLT 2.0 if you use this method.
-
- Posts: 4
- Joined: Wed Jan 23, 2008 2:01 pm
Post by manish_jaiswal »
Hi
I got the solution of the problem. Following XSLT 2.0 code is working to get the required output..
<xsl:for-each-group select="School/student" group-by="name/tokenize(., ';\s*')">
<xsl:sort select="current-grouping-key()"/>
<name count="{count(current-group())}">
<xsl:value-of select="current-grouping-key()"/>
</name>
</xsl:for-each-group>
Thanks for suggestions,
Manish
I got the solution of the problem. Following XSLT 2.0 code is working to get the required output..
<xsl:for-each-group select="School/student" group-by="name/tokenize(., ';\s*')">
<xsl:sort select="current-grouping-key()"/>
<name count="{count(current-group())}">
<xsl:value-of select="current-grouping-key()"/>
</name>
</xsl:for-each-group>
Thanks for suggestions,
Manish
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ 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