XML transformation for web treeview 2.0

Hi,

I have a base XML file that I want to use it as the source for a XMLDatasource object that is feeding a treeView object (.NET 2.0). I've included a sample copy of my base XML below; the XML is representing a tree in a recursive manner. I need to exclude the tags <node> and <Nodes> in the XML while maintaining the relationships between the Val tags, having the following hierarchy in my treeview.

I now this can be done probably using an XLS or while I'm binding the data to my treeView object. Any hint would help,

Thanks,

Robert

DTreeNodeOfOBSTeam

Team0

Team10

Team1001

Team1002

// Sample copy of the base XML

< xml version="1.0" encoding="utf-8" >
<DTreeNodeOfOBSTeam xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<val>
<TeamName>Team0</TeamName>
</val>
<Nodes>
<node>
<val>
<TeamName>Team10</TeamName>
</val>
<Nodes>
<node>
<val>
<TeamName>Team1001</TeamName>
</val>
<Nodes />
</node>
<node>
<val>
<TeamName>Team1002</TeamName>
</val>
<Nodes />
</node>
</Nodes>
</node>
</Nodes>

</DTreeNodeOfOBSTeam>




Answer this question

XML transformation for web treeview 2.0

  • sud

    In your XML sample "val" elements don't have relationship with each other. So without "Nodes" and "node" elements XML become:

    <DTreeNodeOfOBSTeam xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <val>
    <TeamName>Team0</TeamName>
    </val>
    <val>
    <TeamName>Team10</TeamName>
    </val>
    <val>
    <TeamName>Team1001</TeamName>
    </val>
    <val>
    <TeamName>Team1002</TeamName>
    </val>
    </DTreeNodeOfOBSTeam>

    XSLT for this is:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">
    <xsl:copy>
    <xsl:apply-templates select="node()"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="Nodes | node">
    <xsl:apply-templates select="*"/>
    </xsl:template>

    </xsl:stylesheet>

    You may want to try the something like:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="DTreeNodeOfOBSTeam">
    <xsl:call-template name="node" />
    </xsl:template>

    <xsl:template match="node" name="node">
    <Team name="{val/TeamName}">
    <xsl:apply-templates select="Nodes"/>
    </Team>
    </xsl:template>

    </xsl:stylesheet>

    Which gives you:

    <Team name="Team0">
    <Team name="Team10">
    <Team name="Team1001" />
    <Team name="Team1002" />
    </Team>
    </Team>

    To execute XSLT you can use XslCompiledTransform class.




  • Wayne Clements

    Thanks much Sergey. I took me a while to digest it (I'm novice at XML) but I works like a charm.

    Take care,

    Robert



  • Stampede2

    Good luck.

  • XML transformation for web treeview 2.0