 |
| 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
|
|
|
|

January 5th, 2006, 12:46 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
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.
|
|

January 5th, 2006, 05:24 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

January 5th, 2006, 11:29 PM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply.
-ROCXY
|
|

July 15th, 2010, 08:29 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
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.
|
|

July 15th, 2010, 08:37 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

July 15th, 2010, 08:53 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
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.
|
|

July 15th, 2010, 08:59 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|
 |