Flat Employee XML into hierarchical XML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doozer1979
    New Member
    • Jul 2007
    • 3

    #1

    Flat Employee XML into hierarchical XML

    Hello,


    I Have a flat XML file that i have exported from MS access. When i export it from access i want to run it through an xslt to turn it into a hierachical structure

    Each person has a "Reports-To" attribute to identify their manager

    This is what the xml looks like currently:


    <?xml version="1.0"?>
    <dataroot xmlns:od="urn:s chemas-microsoft-com:officedata" >
    <person Employee_x0020_ ID="1" Reports-To="0" Position_x0020_ Title="CEO" Person_x0020_Na me="Kevin McCabe" Grade_x0020_Wor king="CEO" Grade_x0020_Sub st="CEO" Collar_x0020_No ="x" Sector="SUFC" Organisation="C orporate" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="2" Reports-To="1" Position_x0020_ Title="VP" Person_x0020_Na me="Jason Rockett" Grade_x0020_Wor king="VP" Grade_x0020_Sub st="VP" Collar_x0020_No ="x" Sector="SUFC" Organisation="C orporate" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="3" Reports-To="1" Position_x0020_ Title="VP" Person_x0020_Na me="Kevin Blackwell" Grade_x0020_Wor king="Manager" Grade_x0020_Sub st="Manager" Collar_x0020_No ="x" Sector="SUFC" Organisation="C oaching" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="4" Reports-To="3" Position_x0020_ Title="VP" Person_x0020_Na me="Sam Ellis" Grade_x0020_Wor king="Asst_mana ger" Grade_x0020_Sub st="Asst_manage r" Collar_x0020_No ="x" Sector="SUFC" Organisation="C oaching" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="5" Reports-To="3" Position_x0020_ Title="Captain" Person_x0020_Na me="Chris Morgan" Grade_x0020_Wor king="Capt" Grade_x0020_Sub st="Capt" Collar_x0020_No ="x" Sector="SUFC" Organisation="P layers" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="6" Reports-To="3" Position_x0020_ Title="Striker" Person_x0020_Na me="James Beattie" Grade_x0020_Wor king="Sriker" Grade_x0020_Sub st="Sriker" Collar_x0020_No ="x" Sector="SUFC" Organisation="P layers" Cost_x0020_Cent re="26050"></person>

    </dataroot>


    i need it to look like this:


    <?xml version="1.0"?>
    <dataroot xmlns:od="urn:s chemas-microsoft-com:officedata" >
    <person Employee_x0020_ ID="1" Reports-To="0" Position_x0020_ Title="CEO" Person_x0020_Na me="Kevin McCabe" Grade_x0020_Wor king="CEO" Grade_x0020_Sub st="CEO" Collar_x0020_No ="x" Sector="SUFC" Organisation="C orporate" Cost_x0020_Cent re="26050">
    <person Employee_x0020_ ID="2" Reports-To="1" Position_x0020_ Title="VP" Person_x0020_Na me="Jason Rockett" Grade_x0020_Wor king="VP" Grade_x0020_Sub st="VP" Collar_x0020_No ="x" Sector="SUFC" Organisation="C orporate" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="3" Reports-To="1" Position_x0020_ Title="VP" Person_x0020_Na me="Kevin Blackwell" Grade_x0020_Wor king="Manager" Grade_x0020_Sub st="Manager" Collar_x0020_No ="x" Sector="SUFC" Organisation="C oaching" Cost_x0020_Cent re="26050">
    <person Employee_x0020_ ID="4" Reports-To="3" Position_x0020_ Title="VP" Person_x0020_Na me="Sam Ellis" Grade_x0020_Wor king="Asst_mana ger" Grade_x0020_Sub st="Asst_manage r" Collar_x0020_No ="x" Sector="SUFC" Organisation="C oaching" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="5" Reports-To="3" Position_x0020_ Title="Captain" Person_x0020_Na me="Chris Morgan" Grade_x0020_Wor king="Capt" Grade_x0020_Sub st="Capt" Collar_x0020_No ="x" Sector="SUFC" Organisation="P layers" Cost_x0020_Cent re="26050"></person>
    <person Employee_x0020_ ID="6" Reports-To="3" Position_x0020_ Title="Striker" Person_x0020_Na me="James Beattie" Grade_x0020_Wor king="Sriker" Grade_x0020_Sub st="Sriker" Collar_x0020_No ="x" Sector="SUFC" Organisation="P layers" Cost_x0020_Cent re="26050"></person>
    </person>
    </person>
    </dataroot>


    And this is the xslt that i currently trying to achieve the intended results with:



    <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" version="1.0" >

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <!--Changes elements to attributes-->
    <xsl:template match="*[*]"> <!-- copy element with children -->
    <xsl:copy>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>
    <xsl:template match="*[not (*)]">
    <xsl:attribut e name="{name()}" >
    <xsl:value-of select="."/>
    </xsl:attribute>
    </xsl:template>

    <!--Turns flat XML into Hierarchical XML based on 'reports-to' attribute-->
    <xsl:template match="dataroot " >
    <dataroot>
    <xsl:for-each select="person[@Reports-To = '0']">
    <person Employee_x0020_ ID="{@Employee_ x0020_ID}" Person_x0020_Na me="{@Person_x0 020_Name}" Position_x0020_ Title="{@Positi on_x0020_Title} " Grade_x0020_Wor king="{@Grade_x 0020_Working}" Grade_x0020_Sub st="{@Grade_x00 20_Subst}" Collar_x0020_No ="{@Collar_x002 0_No}" Sector="{@Secto r}" Organisation="{ @Organisation}" Cost_x0020_Cent re="{@Cost_x002 0_Centre}">
    <xsl:apply-templates select="//dataroot/person">
    <xsl:with-param name="mgrid" select="@Employ ee_x0020_ID"/>
    </xsl:apply-templates>
    </person>
    </xsl:for-each>
    </dataroot>
    </xsl:template>

    <xsl:template match="dataroot " >
    <xsl:param name="mgrid" />
    <xsl:if test="@Reports-To=$mgrid" >
    <person Employee_x0020_ ID="{@Employee_ x0020_ID}" Person_x0020_Na me="{@Person_x0 020_Name}" Position_x0020_ Title="{@Positi on_x0020_Title} " Grade_x0020_Wor king="{@Grade_x 0020_Working}" Grade_x0020_Sub st="{@Grade_x00 20_Subst}" Collar_x0020_No ="{@Collar_x002 0_No}" Sector="{@Secto r}" Organisation="{ @Organisation}" Cost_x0020_Cent re="{@Cost_x002 0_Centre}">
    <xsl:apply-templates select="//dataroot/person">
    <xsl:with-param name="mgrid" select="@Employ ee_x0020_ID"/>
    </xsl:apply-templates>
    </person>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>


    When i use this xslt on the export from MS Access it wipes out all the nodes apart from the root. When i link to this xslt from the xml, IE6 renders a blank page.

    Can anyone spot where i am going wrong?

    Thank you for your assistance
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Flat Employee XML into hierarchical XML

    I think the error is in the XPaths of the first template. and your last 2 template will be called only once. (there are no dataroot children in the person elements).

    but the more complicated thing is how to program the hierarchy. you can use a fixed algorithm if the input xml suits it. if not you could try to assign each level to a variable (especially if the nodes are not ordered) and then concat the variables to the output xml.

    this is a tough task...

    regards

    Comment

    Working...