[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

parsing a complex xml tree into simple trees (a chall

Subject: parsing a complex xml tree into simple trees (a challenging problem at least for me :)
From: "daghan" <daghanacay@xxxxxxxxxxx>
Date: Tue, 8 Nov 2005 13:14:06 +1100
parsing complex xml
Hi;
 
I was trying to accomplish splitting a composite tree into simple tree. I am
using Java-Xalan processor with no extension, and I belive it supports XSLT
1.0 XPath 1.0. Here is my problem;
 
I have an xml file as follows;
<root> root
    <chops att1="something">1,2<X>XText1,XText2<Y>YT1,YT2</Y></X></chops>
</root>
 
I need to get the result output as follows;
 
<root>
    <chops att1="something">1<X>XText1<Y>YT1</Y></X></chops>
    <chops att1="something">1<X>XText1<Y>YT2</Y></X></chops>
    <chops att1="something">1<X>XText2<Y>YT1</Y></X></chops>
    <chops att1="something">1<X>XText2<Y>YT2</Y></X></chops>
    <chops att1="something">2<X>XText1<Y>YT1</Y></X></chops>
    <chops att1="something">2<X>XText1<Y>YT2</Y></X></chops>
    <chops att1="something">2<X>XText2<Y>YT1</Y></X></chops>
    <chops att1="something">2<X>XText2<Y>YT2</Y></X></chops>
</root>
 
in general this should recursively run on ever element node and split text
in any length seperated with comma (such as XText1,XText2,XText3,XText4,...
instead of XText1,XText2 adn this will be added to result tree). I have
tried couple of recursice agorithms and one such is given below (it is not
working as intended)
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
    <xsl:output method="xml" />
    <xsl:template match="/*">
        <xsl:text>&#xA;</xsl:text>
        <xsl:call-template name="walkTree">
            <xsl:with-param name="currentElement" select="./*"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="walkTree">
        <xsl:param name="currentElement"/>
        <xsl:if test="count($currentElement[child::*])>0">
            <xsl:call-template name="tokenise">
                <xsl:with-param name="processElement"
select="$currentElement" />                    
                <xsl:with-param name="str"
select="normalize-space($currentElement/text())" />
                <xsl:with-param name="delim" select="','" />
            </xsl:call-template>                
            
        </xsl:if>
        <xsl:if test="count($currentElement[child::*])=0">
            <xsl:call-template name="tokenise">
                <xsl:with-param name="processElement"
select="$currentElement" />                    
                <xsl:with-param name="str"
select="normalize-space($currentElement/text())" />
                <xsl:with-param name="delim" select="','" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="tokenise">
        <xsl:param name="processElement" />
        <xsl:param name="str" />
        <xsl:param name="delim" />
        <xsl:choose>
            <xsl:when test="substring-after($str,$delim) != ''">
                <xsl:element name="{name($processElement)}">           
                    <xsl:value-of select="substring-before($str,$delim)" />
                    <xsl:if test="count($processElement[child::*])>0">

                        <xsl:call-template name="walkTree">
                            <xsl:with-param name="currentElement"
select="$processElement/*"/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:element>
                <xsl:call-template name="tokenise">
                    <xsl:with-param name="processElement"
select="$processElement" />
                    <xsl:with-param name="str"
select="substring-after($str,$delim)" />
                    <xsl:with-param name="delim" select="$delim" />     
                </xsl:call-template>
                
            </xsl:when>
            <xsl:when test="substring-after($str,$delim) = ''">
                <xsl:element name="{name($processElement)}">
                    <xsl:value-of select="$str" />
                    <xsl:if test="count($processElement[child::*])>0">

                        <xsl:call-template name="walkTree">
                            <xsl:with-param name="currentElement"
select="$processElement/*"/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:element>
            </xsl:when>
        </xsl:choose>  
    </xsl:template>
</xsl:stylesheet>
 
output to this is 
 
<chops>1<X>XText1<Y>YT1</Y><Y>YT2</Y></X><X>XText2<Y>YT1</Y><Y>YT2</Y></X></
chops>
<chops>2<X>XText1<Y>YT1</Y><Y>YT2</Y></X><X>XText2<Y>YT1</Y><Y>YT2</Y></X></
chops>
 
very close but not here yet. I am using JDK 1.5 I like to stick with my
processor since it is embedded in JDK and easy to use however I am open to
suggestions.
 
cheers 

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.