xsl-list
[Top] [All Lists]

xsl transform flatfile to xml

2005-02-15 23:58:45
hi,
I have an xml file (from an sql query) 

<output>
    <line>
          <c>c1</c>
          <l>l1</l>
          <o>o1</o>
    </line>
    <line>
          <c>c1</c>
          <l>l2</l>
          <o>o1</o>
    </line>
    <line>
          <c>c1</c>
          <l>l1</l>
          <o>o2</o>
    </line>
    <line>
          <c>c1</c>
          <l>l2</l>
          <o>o2</o>
    </line>
    <line>
          <c>c2</c>
          <l>l3</l>
          <o>o2</o>
    </line>
    <line>
          <c>c2</c>
          <l>l4</l>
          <o>o2</o>
    </line>
</output>
 
which i want to transform into an xml in the following format.
================>

<results>
    <comp id="1">
        <c>c1</c>
        <l parent="1" array="1">
            <name>l1</name>
        </l>
        <l parent="1" array="2">
            <name>l2</name>
        </l>
        <o parent="1" array="1">
            <name>o1</name>
        </o>
        <o parent="1" array="2">
            <name>o2</name>
        </o>
    </comp>
    <comp id="2">
        <c>c2</c>
        <l parent="2" array="1">
            <name>l3</name>
        </l>
        <l parent="2" array="2">
            <name>l4</name>
        </l>
        <o>
            <name>o2</name>
        </o>
    </comp>
</results>

The xsl i used was

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:key name="records" match="line" use="c"/>
    
    <xsl:template match="/">
        <results>
            <xsl:apply-templates select="output"/>
        </results>
    </xsl:template>
    
    <xsl:template match="output">
        <xsl:for-each
            select="line[generate-id() =
generate-id(key('records',c)[1])]">
            <xsl:variable name="pos" select="position()"/>
            <comp id="{$pos}">
                <c>
                    <xsl:value-of select="c"/>
                </c>
                
                <xsl:call-template name="L2Interface">
                    <xsl:with-param name="parentid" select="$pos"/>
                </xsl:call-template>
                
                <xsl:call-template name="OSInterface">
                    <xsl:with-param name="parentid" select="$pos"/>
                </xsl:call-template>
                
            </comp>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="OSInterface">
        <xsl:param name="parentid"/>
        <xsl:for-each select="key('records',c)[not(o = preceding::o)]">
            <xsl:variable name="index" select="position()"/>
            
            
            <o array="{$index}" parent="{$parentid}">
                <name>
                    <xsl:value-of select="o"/>
                </name> 
                
            </o>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="L2Interface">
        <xsl:param name="parentid"/>
        
        <xsl:for-each select="key('records',c)[not(l = preceding::l)]">
            
            <xsl:variable name="index" select="position()"/>
        
        
                <l array="{$index}" parent="{$parentid}">
                    <name>
                        <xsl:value-of select="l"/>
                    </name> 
                    
                </l>
        
        </xsl:for-each>
        
    </xsl:template>
</xsl:stylesheet>

but i dont get the <o> elements properly.
Any help!!!!!!!!!!!!!!




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



<Prev in Thread] Current Thread [Next in Thread>