Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2016, 10:37 AM
Authorized User
 
Join Date: Nov 2016
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Post Grouping and sorting content missing issue

Hi,

I have faced some xml grouping element using xsl:for-each-group.

During conversion "<cit>AIR 1981 SC 212</cit>" content was missed out in converetd file.

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<hapmodule><tablecases>
<title>Table of Cases</title>
<Para0>
<carf>
<case>Som Prakash Rekhi v Union of India</case>
<cit>(1981) 1 SCC 449</cit>
</carf>
</Para0>
<Para0><carf>
<cit>AIR 1981 SC 212</cit>
</carf></Para0>
<Para0><carf>
<case>LIC of India v Escorts Ltd</case>
<cit>(1986) 59 Comp Cas 548 (SC)</cit>
</carf></Para0>
<Para0><carf>
<case>Lakatamia Shipping Co Ltd v Su &amp; Others</case>
<cit>[2014] EWCA Civ 636</cit>
</carf></Para0>
<Para0><carf>
<cit>(2015) 1 WLR 291 (CA)</cit>
</carf></Para0>
<Para0><carf>
<case>KRS Mani v Anugraha Jewellers Ltd</case>
<cit>(2004) 52 SCL 488 (Mad)</cit>
</carf></Para0>
<Para0><carf>
<case>Tea Brokers Pvt Ltd v Hemendra Prosad Barooah</case>
<cit>(1998) 5 Com LJ 463 (Cal)</cit>
</carf></Para0>
</tablecases></hapmodule>

XSLT:

<?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="2.0">
<xsl:output method="xml"/>
<!-- <xsl:variable name="file" select="document('extractedTags.xml')"/> -->
<xsl:key name="casetag" match="Para0" use="case.cit/lit"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="hapmodule">
<hapmodule>
<tablecases>
<title>Table of Cases</title>

<xsl:if test="not(following-sibling::tablecases/Para0/carf/case)">
<xsl:element name="case">
<xsl:value-of select="preceding-sibling::case"/>
</xsl:element>
</xsl:if>

<xsl:for-each-group select="tablecases/Para0/carf" group-by="case">
<xsl:sort select="case" />
<para>
<Para0>
<carf>

<case><xsl:value-of select="case"/></case>
<cit><xsl:value-of select="cit"/></cit>
</carf>
</Para0>
</para>
</xsl:for-each-group>

</tablecases>
</hapmodule>
</xsl:template>

Converetd Output:

<?xml version="1.0" encoding="UTF-8"?>
<hapmodule>
<tablecases>
<title>Table of Cases</title>
<case/>
<para>
<Para0>
<carf>
<case>KRS Mani v Anugraha Jewellers Ltd</case>
<cit>(2004) 52 SCL 488 (Mad)</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>LIC of India v Escorts Ltd</case>
<cit>(1986) 59 Comp Cas 548 (SC)</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Lakatamia Shipping Co Ltd v Su &amp; Others</case>
<cit>[2014] EWCA Civ 636</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Som Prakash Rekhi v Union of India</case>
<cit>(1981) 1 SCC 449</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Tea Brokers Pvt Ltd v Hemendra Prosad Barooah</case>
<cit>(1998) 5 Com LJ 463 (Cal)</cit>
</carf>
</Para0>
</para>
</tablecases>
</hapmodule>

Please give the solution.

Thanks,
Bharathi.G
 
Old November 30th, 2016, 10:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First error is at line 21,

Code:
                <xsl:if test="not(following-sibling::tablecases/Para0/carf/case)">
                    <xsl:element name="case">
                        <xsl:value-of select="preceding-sibling::case"/>
                    </xsl:element>
                </xsl:if>
Both following-sibling::tablecases and preceding-sibling::case are selecting relative to the hapmodule element, which suggests that you either (a) haven't understood the importance of tracking the context node, or (b) that you haven't understood the sibling axis. (I can't correct the code because I have no idea what it is trying to do).

Then your actual bug:

Code:
<xsl:for-each-group select="tablecases/Para0/carf" group-by="case">
If an element in the grouping population has no value for the grouping key, that is, if a carf element has no child called case, then it's not going to be put in any group, and therefore it won't be processed. If you want carf elements with no case child to go in a group on their own, try group-by="string(case)".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 1st, 2016, 12:28 AM
Authorized User
 
Join Date: Nov 2016
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Hi mhKay,

Thank you very much for your swift reply. Sorry i forgot to remove the Line 21 this is for trying some other purpose. i changed the code "string(case)" i got the missed content.

But My expected output "<cit>AIR 1981 SC 212</cit>" in this content move to previous group. please check the Expected output..


Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<hapmodule><tablecases>
<title>Table of Cases</title>
<Para0>
<carf>
<case>Som Prakash Rekhi v Union of India</case>
<cit>(1981) 1 SCC 449</cit>
</carf>
</Para0>
<Para0><carf>
<cit>AIR 1981 SC 212</cit>
</carf></Para0>
<Para0><carf>
<case>LIC of India v Escorts Ltd</case>
<cit>(1986) 59 Comp Cas 548 (SC)</cit>
</carf></Para0>
<Para0><carf>
<case>Lakatamia Shipping Co Ltd v Su &amp; Others</case>
<cit>[2014] EWCA Civ 636</cit>
</carf></Para0>
<Para0><carf>
<cit>(2015) 1 WLR 291 (CA)</cit>
</carf></Para0>
<Para0><carf>
<case>KRS Mani v Anugraha Jewellers Ltd</case>
<cit>(2004) 52 SCL 488 (Mad)</cit>
</carf></Para0>
<Para0><carf>
<case>Tea Brokers Pvt Ltd v Hemendra Prosad Barooah</case>
<cit>(1998) 5 Com LJ 463 (Cal)</cit>
</carf></Para0>
</tablecases></hapmodule>

XSLT:

<?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="2.0">
<xsl:output method="xml"/>
<!-- <xsl:variable name="file" select="document('extractedTags.xml')"/> -->
<xsl:key name="casetag" match="Para0" use="case.cit/lit"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="hapmodule">
<hapmodule>
<tablecases>
<title>Table of Cases</title>

<xsl:for-each-group select="tablecases/Para0/carf" group-by="string(case)">
<xsl:sort select="case" />
<para>
<Para0>
<carf>

<case><xsl:value-of select="case"/></case>
<cit><xsl:value-of select="cit"/></cit>
</carf>
</Para0>
</para>
</xsl:for-each-group>

</tablecases>
</hapmodule>
</xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<hapmodule>
<tablecases>
<title>Table of Cases</title>
<case/>
<para>
<Para0>
<carf>
<case/>
<cit>AIR 1981 SC 212</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>KRS Mani v Anugraha Jewellers Ltd</case>
<cit>(2004) 52 SCL 488 (Mad)</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>LIC of India v Escorts Ltd</case>
<cit>(1986) 59 Comp Cas 548 (SC)</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Lakatamia Shipping Co Ltd v Su &amp; Others</case>
<cit>[2014] EWCA Civ 636</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Som Prakash Rekhi v Union of India</case>
<cit>(1981) 1 SCC 449</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Tea Brokers Pvt Ltd v Hemendra Prosad Barooah</case>
<cit>(1998) 5 Com LJ 463 (Cal)</cit>
</carf>
</Para0>
</para>
</tablecases>
</hapmodule>

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<hapmodule>
<tablecases>
<title>Table of Cases</title>
<case/>
<para>
<Para0>
<carf>
<case/>

</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>KRS Mani v Anugraha Jewellers Ltd</case>
<cit>(2004) 52 SCL 488 (Mad)</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>LIC of India v Escorts Ltd</case>
<cit>(1986) 59 Comp Cas 548 (SC)</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Lakatamia Shipping Co Ltd v Su &amp; Others</case>
<cit>[2014] EWCA Civ 636</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Som Prakash Rekhi v Union of India</case>
<cit>(1981) 1 SCC 449</cit>
<cit>AIR 1981 SC 212</cit>
</carf>
</Para0>
</para>
<para>
<Para0>
<carf>
<case>Tea Brokers Pvt Ltd v Hemendra Prosad Barooah</case>
<cit>(1998) 5 Com LJ 463 (Cal)</cit>
</carf>
</Para0>
</para>
</tablecases>
</hapmodule>

Last edited by bharathidasang; December 1st, 2016 at 12:31 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing Content? & and | kkinderen BOOK: Beginning C# 6.0 Programming with Visual Studio 2015 1 March 26th, 2017 04:26 PM
Sorting with missing attributes mnt581 XSLT 1 March 30th, 2012 12:40 PM
Missing mail content with CDONTS sunile-zest Classic ASP Basics 1 July 10th, 2007 02:22 PM
Grouping and (dynamic) sorting asearle XSLT 12 November 10th, 2006 10:51 AM
1:M sorting or grouping cant explain :: newbie Q auser99 XSLT 4 April 29th, 2005 11:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.