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 November 18th, 2009, 03:38 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 2.0 stylesheet assuming you have those 'para' elements wrapped in a 'root' element:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xsd">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="root">
    <xsl:copy>
      <xsl:for-each-group select="para" group-by="tag/@name">
        <mainlist name="{current-grouping-key()}">
          <xsl:for-each select="current-group()/paraline/string">
            <list><text><xsl:value-of select="."/></text></list>
          </xsl:for-each>
        </mainlist>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
sushil.sharma75 (November 18th, 2009)
 
Old November 18th, 2009, 03:39 PM
Registered User
 
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Sorry for the wrong XML the correct are

Code:
<para>
			<tag name="bullet"/>
			<paraline>
				<string>One</string>
			</paraline>
		</para>
		<para>
			<tag name="bullet"/>
			<paraline>
				<string>Two</string>
			</paraline>
		</para>
		<para>
			<tag name="Number"/>
			<paraline>
				<string>11111</string>
			</paraline>
		</para>
		<para>
			<tag name="Number"/>
			<paraline>
				<string>22222</string>
			</paraline>
		</para>
output

Code:
<mainlist name="bullet">
			<list>
				<text>One</text>
			</list>
			<list>
				<text>Two</text>
			</list>
		</mainlist>
		<mainlist name="Number">
			<list>
				<text>11111</text>
			</list>
			<list>
				<text>22222</text>
			</list>
		</mainlist>

I am using XSLT 2.0
 
Old November 20th, 2009, 01:47 PM
Registered User
 
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Here is an XSLT 2.0 stylesheet assuming you have those 'para' elements wrapped in a 'root' element:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xsd">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="root">
    <xsl:copy>
      <xsl:for-each-group select="para" group-by="tag/@name">
        <mainlist name="{current-grouping-key()}">
          <xsl:for-each select="current-group()/paraline/string">
            <list><text><xsl:value-of select="."/></text></list>
          </xsl:for-each>
        </mainlist>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Thank you very much for the quick reply.
The group-by does not solve my problem. The problem comes when the para no tags element. The out put should be simple Paraline/String text should be in <text> tags only like
Code:
<para>
	<tag name="bullet"/>
	<paraline>
	 	<string>One</string>
	</paraline>
</para>
<para>
	<tag name="bullet"/>
	<paraline>
		<string>Two</string>
	</paraline>
</para>
<para>
	<paraline>
		<string>THANK YOU</string>
	</paraline>
</para>
<para>
	<tag name="Number"/>
	<paraline>
		<string>11111</string>
	</paraline>
</para>
<para>
	<tag name="Number"/>
	<paraline>
		<string>22222</string>
	</paraline>
</para>
output should be

Code:
<mainlist name="bullet">
	<list>
		<text>One</text>
	</list>
	<list>
		<text>Two</text>
	</list>
</mainlist>
<text>THANK YOU</text>
<mainlist name="Number">
	<list>
		<text>11111</text>
	</list>
	<list>
		<text>22222</text>
	</list>
</mainlist>
 
Old November 20th, 2009, 01:59 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a slight adaption of the stylesheet that produces the output you described for the latest input sample you posted:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xsd">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="root">
    <xsl:copy>
      <xsl:for-each-group select="para" group-adjacent="exists(tag/@name)">
        <xsl:choose>
          <xsl:when test="current-grouping-key()">
            <xsl:for-each-group select="current-group()" group-adjacent="tag/@name">
              <mainlist name="{current-grouping-key()}">
                <xsl:for-each select="current-group()/paraline/string">
                  <list><text><xsl:value-of select="."/></text></list>
                </xsl:for-each>
              </mainlist>
            </xsl:for-each-group>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="current-group()/paraline/string">
              <text><xsl:value-of select="."/></text>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; November 20th, 2009 at 02:06 PM.. Reason: slight correction of the stylesheet to handle several adjacent para elements without a tag/@name attribute





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed to convert an XML file rpalmer68 XSLT 3 June 20th, 2009 11:45 PM
Subject: Convert XML using XSLT manoj1984 XSLT 6 May 30th, 2008 04:27 PM
Using XSLT to convert XML to a table ? nobitavn94 XSLT 3 October 30th, 2006 11:03 AM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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