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, 05:40 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Using - "group-adjacent"

Hi All,

I have a XML.

\artbody\body1\section\procplace following proctopic
\artbody\procplace following proctopic
\artbody\section\procplace following proctopic

<procplace>----------</procplace>
<proctopic> ---------</proctopic>
<procplace>----------</procplace>
<proctopic> ---------</proctopic>

similarly

\artbody\body1\section\quest following answer
\artbody\quest following answer
\artbody\section\quest following answer


<quest>...........</quest>
<answer>........</answer>
<quest>...........</quest>
<answer>........</answer>
<quest>...........</quest>
<answer>........</answer>


Following is the XSL used

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

As you see - I was trying to apply elements <meetings> for group-adjacent ="self:: procplace or self:: proctopic " and <quesans> for group-adjacent ="self::quest or self::answer” . But the above xsl fails and also I was try using declaring all the four inside one group-adjacent and group-by …..?

Thanks & Regards,
- ROCXY



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

I think it would be useful if you supplied a complete XML source document and a complete stylesheet, and an example of the output you want (and the output you are getting), because it's hard to see from these fragments what's going wrong. Cut it down to a manageable size and complexity, so it focuses on the one thing that you're having problems with. Also, it's not helpful to say that something "fails" without saying what the symptoms of failure are.

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

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




 
Old January 4th, 2006, 10:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Neither your source XML nor your stylesheet are well-formed XML.

You're not making it easy for anyone to help you.

The <xsl:choose> and <xsl:when> at the start of your template rule, apart from having no end tags, look like nonsense.

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

Dear Mr. Michael,

I sincerely apologies for the confusion.

This is what I am expecting.

Two different group in different parts\nodes\childs of XML, let us consider
Group:1 (<a> and <b>)
Group:2 (<c> and <d> )

I have to fetch it from all way from XML start to end as group.

And form like.

<ab>
<a>-------</a>
<b>-------</b>
<a>-------</a>
<b>-------</b>
<a>-------</a>
<b>-------</b>
</ab>

<cd>
<c>---------</c>
<d>---------</d>
<c>---------</c>
<d>---------</d>
<c>---------</c>
<d>---------</d>
</cd>

I tried as suggested earlier with group-adjacent.

Please do not ignore - help me..

Thanks & Regards,
-ROCXKY







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 Two "group-adjacent" ROCXY XSLT 0 January 4th, 2006 08:29 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.