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 January 6th, 2006, 05:40 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Replacing elements name while grouping.

Hi All,

I have an XML.

<artbody>
<sec>
 <heading1>
        <verse>Kya, karein mere n;)</verse>
        <verse>Kya, old angry man hoon ;)</verse>
   </heading1>
</sec>
<sec>
  <subsec>
        <verse>Kya, karein mere n;)</verse>
        <verse>Kya, old angry man hoon ;)</verse>
  </sebsec>
</sec>
</artbody>

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

<xsl:template match="verse">
  <xsl:if test="not(name(preceding-sibling::*[1]) = 'verse')">
    <quote>
    <xsl:copy-of select=".|following-sibling::verse"/>
    </quote>
  </xsl:if>
</xsl:template>

<xsl:template match="verse">
<xsl:copy>
<para><xsl:apply-templates/></para>
</xsl:copy>
</xsl:template>

Output XML.
-----------
<artbody>
   <sec>
      <heading1>
        <verse>
            <para>Kya, karein mere n;)</para>
         </verse>
        <verse>
            <para>Kya, old angry man hoon ;)</para>
         </verse>
      </heading1>
   </sec>
   <sec>
      <subsec>
        <verse>
            <para>Kya, karein mere n;)</para>
         </verse>
        <verse>
            <para>Kya, old angry man hoon ;)</para>
         </verse>
      </subsec>
   </sec>
</artbody>
---------------------------------------------------------------------

As you see while grouping 'verse', I would like to change child <verse> to <para> and this should happen after grouping all child 'verse' inside 'quote' like this....

....
<quote>
<para>Kya, karein mere n;)</para>
<para>Kya, old angry man hoon ;)</para>
</quote>
......

I would greatly appreciate any help provided.

Thanks & Regards,
- ROCXY


__________________
Thanks,
Rocxy.
 
Old January 6th, 2006, 09:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You have two template rules that match verse. The XSLT spec is a bit ambivalent on this: it says that it's an error, but the processor doesn't have to report the error, it can recover by choosing the last rule that matches.

What you are trying to achieve is to process the verse twice. The way to do that is with a multi-phase transformation (a mini pipeline). Capture the result of the first transformation in a variable

<xsl:variable name="temp">
  <xsl:apply-templates/>
</xsl:variable>

then apply templates to this, in a different mode:

<xsl:apply-templates select="$temp" mode="phase-2"/>

Only, in an XSLT 1.0 processor, you may have to call the vendor's xx:node-set() extension function to make this work:

<xsl:apply-templates select="xx:node-set($temp)" mode="phase-2"/>

where the namespace bound to xx depends on the XSLT processor you are using.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 6th, 2006, 12:52 PM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Dear kay,

I was trying this syntax but - I dont know how to frame it exactly inside the XSL.

Thanks & Regards,
-ROCXY

 
Old January 6th, 2006, 01:11 PM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Kay,

I used the following code its working fine.

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

<xsl:template match="verse">
  <xsl:if test="not(name(preceding-sibling::*[1]) = 'verse')">
    <quote>
    <xsl:apply-templates select=". | following-sibling::verse" mode="phase1"/>
    </quote>
  </xsl:if>
</xsl:template>

<xsl:variable name = "temp">
<xsl:apply-templates select="node() | @*" mode="phase1"/>
</xsl:variable>

<xsl:template match="verse" mode="phase1">
<para>
<xsl:apply-templates />
</para>
</xsl:template>

Thanks
-ROCXY






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT - Grouping by Attribute but same elements vinaura XSLT 2 July 18th, 2007 10:34 PM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
Grouping orphan nodes to paranet elements ROCXY XSLT 2 January 1st, 2006 07:11 AM
Grouping Elements using XSLT mathuranuj XSLT 2 June 21st, 2005 02:56 AM





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