Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 16th, 2007, 11:26 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Merging nodes

Hello

I'm trying to merge nodes inside <book.content> node. How it works is that only when a <book.content> node is preceded by node named <close> then <book.content> gets closed and a new <book.content> starts. Otherwise <book.content> goes on merging whatever comes in it's way.

Note: There might be many occurences of <close> node at other places but we have to check only for ones who have next node as <book.content>.

<books>
 <book.content>
   <Topic>ABC</Topic>
   <author>123</author>
 </book.content>

 <book.content>
   <Topic>DEF</Topic>
   <author>456</author>
 </book.content>

  <close>
    <Index>aaa</index>
    <pages>111</pages>
  </close>

  <Noname>
    <useless>one</useless>
  </Noname>

  <close>
    <Index>bbb</index>
    <pages>222</pages>
  </close>

  <book.content>
    <Topic>GHI</Topic>
    <author>789</author>
  </book.content>
<books>


Output:
<books>
 <book.content>
   <Topic>ABC</Topic>
   <author>123</author>
 <s></book.content></s>

 <s><book.content></s>
   <Topic>DEF</Topic>
   <author>456</author>
 <s></book.content></s>

  <close>
    <Index>aaa</index>
    <pages>111</pages>
  </close>

  <Noname>
    <useless>one</useless>
  </Noname>

  <close>
    <Index>bbb</index>
    <pages>222</pages>
  </close>

  <s><book.content></s>
    </book.content>

  <book.content>
    <Topic>GHI</Topic>
    <author>789</author>
  </book.content>
<books>

Here, the last <book.content> does not have any following <close> node after it so it can be closed without merging other nodes.

Reply With Quote
  #2 (permalink)  
Old October 17th, 2007, 10:40 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

anybody..any idea how to proceed with it in ver1.0.



Reply With Quote
  #3 (permalink)  
Old October 17th, 2007, 11:13 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Am I in the right direction...plz help

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

<xsl:template match="//book.content">
        <xsl:choose>
            <xsl:when test="preceding-sibling[node()='close']">
                <xsl:element name="/book.content">
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
</xsl:stylesheet>

Reply With Quote
  #4 (permalink)  
Old October 17th, 2007, 11:45 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sorry, but positional markup can be difficult to process. Any good reason why you're stuck with version 1.0?

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old October 17th, 2007, 11:46 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's a positional grouping problem, much easier in 2.0 using xsl:for-each-group group-starting-with. In 1.0 it's best tackled using sibling recursion and is much trickier, when I'm teaching this exercise usually takes a couple of hours and people get very confused by it.

The general approach is

(a) the template for the parent element does apply-templates to its first child,

(b) the template for the first child in a group does

<group>
  <apply-templates select="following-sibling::*[1]"/>
</group>
<apply-templates select="following-sibling::close/following-sibling::*[1]"/>

(c) the template for the close element does nothing (to terminate the recursion)

(d) the template for every other child element outputs itself then applies templates to its immediately following sibling.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old October 17th, 2007, 01:17 PM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay...points taken that I should now change to ver 2.0

Can somebody direct me to a good example of "xsl:for-each-group group-starting-with"? tried to google and there are too many...too confusing for a beginner.

Or can somebody try to solve the problem I have here?

Reply With Quote
  #7 (permalink)  
Old October 17th, 2007, 01:26 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Can somebody direct me to a good example of "xsl:for-each-group group-starting-with"?

Sure. Since you're on a Wrox forum, read my sig.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #8 (permalink)  
Old October 17th, 2007, 11:37 PM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mike...I've already noticed your siggy and am really impressed that you actually post replies here.

Anyway after little bit of reading I tried doing this but ofcourse it's not correct and need help here.


?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

<xsl:template match="books">
 <xsl:for-each-group select="*" group-by="book.content">
   <book.content>
    <xsl:value-of select="current-group()/book.content">
       <xsl:apply-templates select="."/>
    </xsl:value-of>
   <xsl:if test="preceding-sibling[name()='close']">
     </book.content>
   </xsl:if>
 </xsl:for-each-group>
</xsl:template>

    <xsl:template match="close">
    <close>
        <xsl:apply-templates/>
    </close>
    </xsl:template>

Reply With Quote
  #9 (permalink)  
Old October 18th, 2007, 03:43 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't output half an element (i.e. the start or end tag) seperately. You're not writing text to the output, you are writing nodes to a result node tree - so there is no such thing has half a node. This means your <if></book.content></if> wont work.

You could try using group-ending-with="close" then you will have a group of items, the last of which will always be a close element.



/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #10 (permalink)  
Old October 18th, 2007, 05:07 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You basically want something like

<xsl:for-each-group select="*" group-ending-with="close">
  <book.content>


  </book.content>
</xsl:for-each-group>

But before you try coding it, please, do some reading. So far, you seem to be relying on guesswork.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging several tables woggysmith Access 2 December 12th, 2007 09:55 AM
merging projects Bill_Thompson Beginning VB 6 1 April 26th, 2006 04:58 PM
Merging XML Help Please sam666 XML 7 March 8th, 2006 02:50 AM
Merging two xslts into one...help!! nrane26 XSLT 1 January 7th, 2005 04:21 PM
Merging Data Mitch SQL Server 2000 0 January 2nd, 2004 05:42 PM



All times are GMT -4. The time now is 03:16 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.