xsl-list
[Top] [All Lists]

parsing a complex xml tree into simple trees (a challenging problem at least for me :)

2005-11-07 19:14:06
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 
 


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--