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 October 16th, 2007, 11:26 AM
Authorized User
 
Join Date: Oct 2007
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.

 
Old October 17th, 2007, 10:40 AM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



 
Old October 17th, 2007, 11:13 AM
Authorized User
 
Join Date: Oct 2007
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>

 
Old October 17th, 2007, 11:45 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old October 17th, 2007, 11:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old October 17th, 2007, 01:17 PM
Authorized User
 
Join Date: Oct 2007
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?

 
Old October 17th, 2007, 01:26 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old October 17th, 2007, 11:37 PM
Authorized User
 
Join Date: Oct 2007
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>

 
Old October 18th, 2007, 03:43 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old October 18th, 2007, 05:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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





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





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