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 5th, 2006, 12:46 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Xsl: strip-space elements; and 'group-adjacent'

Hi All,

I have an XML,

<?xml version="1.0"?>
<artbody>
       <section id="s1">

           <quest>What's your name?</quest>
           <quest>What's your name?</quest>
           <answer>Rocxky</answer>
           <answer>Rocxky</answer>
       </section>
</artbody>

Used XSL.
---------
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

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

Observed Output
---------------
<?xml version="1.0" encoding="UTF-8"?>
<artbody>
   <section>
      <QandA>
         <quest>What's you name?</quest>
         <quest>What's you name?</quest>
         <answer>Rocxky</answer>
         <answer>Rocxky</answer>
      </QandA>
   </section>
</artbody>

As you see <xsl:strip-space elements="*"/> the attribute value "s1" of <section id="s1"> to <section>.

Kindly suggest us how to convert section with attributes as it is to the target XML.

Thanks & Regards,
-ROCXKY


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

After

<xsl:copy>

add

<xsl:copy-of select="@*"/>

if you want to copy the attributes unchanged.

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

Thanks for the reply.

-ROCXY

 
Old July 15th, 2010, 08:29 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Thumbs up Xsl: strip-space elements; and 'group-adjacent'

Hi All,

How can we handle multiple template-match (more than 1 match) for below scenario:

XML INPUT

<?xml version="1.0"?>
<artbody>
<section>
<A>What's your name?</A>
<A>What's your name?</A>
<B>Rocxy</B>
<B>Rocxy</B>

<C>What's your Bushiness?</C>
<C>What's your Bushiness?</C>
<D>XML Research</D>
<D>XML Research</D>
</section>
</artbody>


XML OUTPUT
<section>
<1>
<A>What's your name?</A>
<A>What's your name?</A>
<B>Rocxy</B>
<B>Rocxy</B>
</1>

<2>
<C>What's your Bushiness?</C>
<C>What's your Bushiness?</C>
<D>XML Research</D>
<D>XML Research</D>
</2>


</section>

Any help would be grateful.
__________________
Thanks,
Rocxy.
 
Old July 15th, 2010, 08:37 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

An element name is not allowed to start with a digit so the output you want is not possible.
Also explain what determines the grouping exactly, why are the A and B elements in one group and the C and D elements in a second group?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 15th, 2010, 08:53 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Martin Honnen

Thanks for swift response. <1> stands for Personal group say <PG> and <2> for business group say <BG>

Sorry for using digits as an exhibits...

Any help would be great full
__________________
Thanks,
Rocxy.
 
Old July 15th, 2010, 08:59 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Does
Code:
<xsl:template match="section">
   <xsl:copy>
      <PG><xsl:copy-of select="A, B"/></PG>
      <BG><xsl:copy-of select="C, D"/></BG>
   </xsl:copy>
</xsl:template>
do what you want?
I am not sure what your grouping criteria are and what problem you face, with the post mentioning strip-space and group-adjacent without any details.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to group xsl elements in xslt.10 suji XSLT 1 February 29th, 2008 03:44 AM
Using - "group-adjacent" ROCXY XSLT 4 January 4th, 2006 11:09 AM
Using Two "group-adjacent" ROCXY XSLT 0 January 4th, 2006 08:29 AM
Usage of "xsl:strip-space" ROCXY XSLT 1 January 4th, 2006 05:31 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.