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 February 15th, 2013, 06:01 PM
Registered User
 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Split XML using xslt

Hello is it possible to use xslt to go from

Code:
<XML>
  <TAG1>tag1value1;tag1value2</TAG1> 
  <TAG2>tag2value1;tag2value2</TAG2> 
</XML>
To this

Code:
<XML>
    <LINE>
	<TAG1>tag1value1</TAG1> 
	<TAG2>tag2value1</TAG2> 
     </LINE>
     <LINE>
	<TAG1>tag1value2</TAG1> 
	<TAG2>tag2value2</TAG2> 
     </LINE>
</XML>
Any help would be most appreciated.

Johan
 
Old February 16th, 2013, 06:53 AM
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 (that can be used with XSLT 2.0 processors like Saxon 9 or AltovaXML or XmlPrime):
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="XML">
  <xsl:copy>
    <xsl:for-each-group select="*/tokenize(., ';')" group-by="position() mod 2">
      <LINE>
        <xsl:for-each select="current-group()">
          <xsl:element name="TAG{position()}">
            <xsl:value-of select="."/>
          </xsl:element>
        </xsl:for-each>
      </LINE>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 16th, 2013, 04:37 PM
Registered User
 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Martin,

Thanks for your reply, it is working as i asked perfectly.

Can you explain to me how to do the same if the tags are different like this:

Code:
<XML>
  <Name>tag1value1;tag1value2</Name> 
  <Adress>tag2value1;tag2value2</Adress> 
</XML>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Split large XML to Smaller XML using XSLT chilly XSLT 30 March 8th, 2013 09:43 AM
C# XML File Split rangeshram C# 2005 6 May 27th, 2010 08:30 AM
Split large XML to smalls nzvtvsky9 XSLT 1 January 13th, 2010 01:48 PM
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 06:26 AM
XSLT 1.0 Split one Field into Two stlyz3 XSLT 3 February 23rd, 2009 03:42 PM





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