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 July 25th, 2009, 09:47 AM
Authorized User
 
Join Date: Feb 2009
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default Grouping problem

Hi,

I'm facing grouping problem in one of my project. I have to make a group of each "ce-def-term" and "ce-def-description". The condition is "ce-def-description" can appear more than one time.

Could you please look into this and suggest.

Thanks,
Anil Yadav

Input xml
Code:
  <ce-def-list id="deflist1">
   <ce-def-term id="defterm1">adverse effects,</ce-def-term>
   <ce-def-description><ce-para id="para26"><ce-cross-ref refid="p9">p. 9</ce-cross-ref></ce-para></ce-def-description>
   <ce-def-description><ce-para id="para26a"><ce-cross-ref refid="p9">p. 99</ce-cross-ref></ce-para></ce-def-description>
   <ce-def-term id="defterm2">allopathic medicine,</ce-def-term>
   <ce-def-description><ce-para id="para27"><ce-cross-ref refid="p2">p. 2</ce-cross-ref></ce-para></ce-def-description>
   <ce-def-term id="defterm3">chemical name,</ce-def-term>
   <ce-def-description><ce-para id="para28"><ce-cross-ref refid="p4">p. 4</ce-cross-ref></ce-para></ce-def-description>
   <ce-def-term id="defterm4">collaborative problems,</ce-def-term>
   <ce-def-description><ce-para id="para29"><ce-cross-ref refid="p10">p. 10</ce-cross-ref></ce-para></ce-def-description>
   <ce-def-description><ce-para id="para29a"><ce-cross-ref refid="p10">p. 100</ce-cross-ref></ce-para>
   </ce-def-description>
  </ce-def-list>
Required output xml
Code:
      <dl>
         <defitem>
            <dt id="defterm1">adverse effects,</dt>
            <dd>p. 9</dd>
            <dd>p. 99</dd>
         </defitem>
         <defitem>
            <dt id="defterm2">allopathic medicine,</dt>
            <dd>p. 2</dd>
         </defitem>
         <defitem>
            <dt id="defterm3">chemical name,</dt>
            <dd>p. 4</dd>
         </defitem>
         <defitem>
            <dt id="defterm4">collaborative problems,</dt>
            <dd>p. 10</dd>
            <dd>p. 100</dd>
         </defitem>
      </dl>
XSLT Code
Code:
<xsl:template match="ce-def-list">
 <dl>
   <xsl:apply-templates/>
 </dl>
</xsl:template>
<xsl:template match="ce-def-list/ce-def-term">
 <defitem>
  <dt id="{@id}">
   <xsl:apply-templates/>
  </dt>
  <xsl:for-each select="following-sibling::*">
    <xsl:if test="local-name()='ce-def-description' and not(name(following-sibling::*[0])='ce-def-term')">
     <dd>
      <xsl:value-of select="."/>
     </dd>
    </xsl:if>
  </xsl:for-each>
 </defitem>
</xsl:template>
 
Old July 25th, 2009, 10:05 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Positional predicates in XPath start at 1, not 0.

The following works, although there is probably a better way:

Code:
<xsl:template match="ce-def-term">
 <defitem>
 <dt id="{@id}">
  <xsl:apply-templates/>
 </dt>
 <xsl:variable name="id" select="@id"/>
 <xsl:for-each select="following-sibling::ce-def-description">
   <xsl:if test="preceding-sibling::ce-def-term[1]/@id=$id">
    <dd>
     <xsl:value-of select="."/>
    </dd>
   </xsl:if>
 </xsl:for-each>
 </defitem>
</xsl:template>

<xsl:template match="ce-def-description"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 25th, 2009, 11:41 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you or can you use XSLT 2.0? Then the following stylesheet should do what you describe:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="ce-def-list">
    <dl>
      <xsl:for-each-group select="*" group-starting-with="ce-def-term">
        <defitem>
          <dt id="{@id}"><xsl:value-of select="."/></dt>
          <xsl:for-each select="current-group() except .">
            <dd><xsl:value-of select="."/></dd>
          </xsl:for-each>
        </defitem>
      </xsl:for-each-group>
    </dl>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 27th, 2009, 06:19 AM
Authorized User
 
Join Date: Feb 2009
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Default

Hello Sam and Martin,

Thanks a lot for your reply. Now I am getting the desired results.
People like you make the world a better place.

Thanks again,
Anil Yadav





Similar Threads
Thread Thread Starter Forum Replies Last Post
grouping problem at xslt1.0 joshuaa XSLT 4 July 15th, 2008 08:08 AM
XML Grouping Problem andymidd XSLT 2 July 10th, 2007 02:48 AM
Grouping problem bonekrusher XSLT 3 April 7th, 2007 05:11 PM
Grouping problem aware Access 11 August 4th, 2005 11:00 AM
grouping and expressions problem parityah BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 May 2nd, 2005 06:43 PM





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