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 4th, 2006, 08:29 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Using Two "group-adjacent"

Input XML:
---------
<artbody>
<section id="s1">
<heading1 level="1">INTRODUCTION</heading1>
<para>xxx <b>xxx</b> xxxxxxxxxxxxxxxxxxx</para>

<subsect id="s1-1">
<heading2 level="2">Linkends</heading2>
<para>This paragraph contains fixed figure &lt;fx1&gt;, &lt;fx2&gt;, &lt;fx3&gt;.</para>
<para>This paragraph contains linkend type for Bibliography (numbered) (type=bib): [<link type="bib" linkend="">3</link>], [<link type="bib" linkend="">4, 7</link>], [<link type="bib" linkend="">7–12</link>], [<link type="bib" linkend="">14-18</link>], [<link type="bib" linkend="">3, 5, 7–12</link>] and [<link type="bib" linkend="">6, 8, 15-17</link>].</para></subsect1>

                            <subsect1 id="s1-5-1">
                            <heading3 level="3">Question and Answer</heading3>
                            <quest>What's you name?</quest>
                            <answer>Pradeep</answer>
                            <quest>How old are you?</quest>
                            <answer>62</answer>
                            <quest>So, what are you doing now?</quest>
                            <answer>Just chatting with an idiot</answer> </subsect>
                            </subsect>


            <subsect1 id="s1-5-1">
            <heading3 level="3">poem</heading3>
            <stanza>What's you name?</stanza>
            <stanza>Pradeep</stanza>
            </subsect>


<para>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</para>
 <para>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</para>
</section>
</artbody>

expected output xml:
--------------------

        <artbody>
                                    <section id="s1">
                                    <heading1 level="1">INTRODUCTION</heading1>
                                    <para>xxx <b>xxx</b> xxxxxxxxxxxxxxxxxxx</para>

                                    <subsect id="s1-1">
                                    <heading2 level="2">Linkends</heading2>
                                    <para>This paragraph contains fixed figure &lt;fx1&gt;, &lt;fx2&gt;,
                                &lt;fx3&gt;.</para>
                                    <para>This paragraph contains linkend type for Bibliography (numbered)
                                        (type=bib): [<link type="bib" linkend="">3</link>], [<link type="bib"
                                        linkend="">4, 7</link>], [<link type="bib" linkend="">7–12</link>],
                                        [<link type="bib" linkend="">14-18</link>], [<link type="bib"
                                        linkend="">3, 5, 7–12</link>] and [<link type="bib" linkend="">6, 8, 15-
                                        17</link>].</para></subsect1>

                                    <subsect1 id="s1-5-1">
                                    <heading3 level="3">Question and Answer</heading3>

<qanda>
                                    <quest>What's you name?</quest>
                                    <answer>Pradeep</answer>
                                    <quest>How old are you?</quest>
                                    <answer>62</answer>
                                    <quest>So, what are you doing now?</quest>
                                    <answer>Just chatting with an idiot</answer>
</qanda>
                                     </subsect>
                              </subsect>


                                     <subsect1 id="s1-5-1">
                                        <heading3 level="3">poem</heading3>



<poem>
            <stanza>What's you name?</stanza>
            <stanza>Pradeep</stanza>

</poem>
</subsect>


 <para>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</para>
 <para>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</para>
</section>
</artbody>


Style Sheet:
-----------

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
  xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  exclude-result-prefixes="aml w wx o v">

<xsl:output method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="artbody">
 <xsl:copy>
  <xsl:for-each-group select="child::node()" group-adjacent="self::quest or self::answer">
    <xsl:choose>
      <xsl:when test="current-grouping-key()">
        <quanta>
          <xsl:apply-templates select="current-group()"/>

        </quanta>
      </xsl:when>
         <xsl:otherwise>
         <xsl:apply-templates select="current-group()"/>
         </xsl:otherwise>
   </xsl:choose>
   </xsl:for-each-group>
  </xsl:copy>

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

Questions:
---------

1. I would like to group 'quest' & 'answer' under 'qanda'
2. I would like to group 'stanza' under 'poem'

I want to read each element dynamically and should group the orphan nodes such as quest & stanza under another parent element. But, with your suggestion, I could group one at a time. I couldn't do it in a loop. When I tried, I am getting duplicate of orphan nodes and all the attribute values are discarded.

If you could help us to design a xslt that groups "quest & answer" and "stanza" dynamically and retains all other elements untouched.

I sincerely appreciate your help in this regard.

Thanks & Regards,
- ROCXY


__________________
Thanks,
Rocxy.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Xsl: strip-space elements; and 'group-adjacent' ROCXY XSLT 6 July 15th, 2010 08:59 AM
Restart new group number in Group Footer sukarso Crystal Reports 2 October 13th, 2006 12:11 PM
Finding Adjacent Empty Cell keithd Excel VBA 7 July 19th, 2006 06:20 AM
Using - "group-adjacent" ROCXY XSLT 4 January 4th, 2006 11:09 AM
Combining adjacent elements with the same name susanne_broeenhorst XSLT 2 March 10th, 2005 03:49 AM





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