views:

45

answers:

2

Hello, please I was wondering if someone can please help as its quite urgent. I need to convert the structue of an xml file to another xml structure so that I can bind it to a asp.net treeview control (i'm a c# developer). I noticed the asp.net treeview control accepts a transform file or xpath expression and am wondering if some one knows of a solution that will work please: From

<Skeleton>
 <Category>Carto</Category>
 <SubCategoryName>ET-ET-RS23</SubCategoryName>
 <Filename>V-01.XML</Filename>
 <XmlDefinition>SKELETON</XmlDefinition>
</Skeleton> 

<Skeleton>
 <Category>Carto
  <SubCategoryName>ET-ET-RS23
   <Filename>V-01.XML
    <XmlDefinition>&lt;SKELETON /&gt;</XmlDefinition>
   </Filename>
  </SubCategoryName>
 </Category>
</Skeleton>

Basically the I want to have a nested tree structure so I can simply bind to my treeview control. So Category contains SubCategoryName and that contains Filename and that contains xmldefinition

sorry I hope this makes sense, thank you

+1  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:apply-templates select="node()[1]|
                                         following-sibling::node()[1]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="XmlDefinition/text()">
        <xsl:value-of select="concat('&lt;',.,'/&gt;')"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<Skeleton>
    <Category>Carto
        <SubCategoryName>ET-ET-RS23
            <Filename>V-01.XML
                <XmlDefinition>&lt;SKELETON/&gt;</XmlDefinition>
            </Filename>
        </SubCategoryName>
    </Category>
</Skeleton>
Alejandro
+1 to you and @Dimitre for matching each other's solutions so closely. Great minds think alike. ;-)
LarsH
@LarsH, @Alejandro: If two different minds (as Alejandro's and mine most certainly are) come to almost the same solution, this is the truth.
Dimitre Novatchev
Ja! I really appreciate your comments, but I don't think of myself as a great mind. ;)
Alejandro
+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()">
  <xsl:copy>
   <xsl:apply-templates select="node()[1]"/>
   <xsl:apply-templates select="following-sibling::node()[1]"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="XmlDefinition/text()">
  &lt;<xsl:value-of select="."/>/&gt;
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Skeleton>
 <Category>Carto</Category>
 <SubCategoryName>ET-ET-RS23</SubCategoryName>
 <Filename>V-01.XML</Filename>
 <XmlDefinition>SKELETON</XmlDefinition>
</Skeleton>

produces the wanted result:

<Skeleton>
    <Category>Carto
        <SubCategoryName>ET-ET-RS23
            <Filename>V-01.XML
                <XmlDefinition>
  &lt;SKELETON/&gt;
                </XmlDefinition>
            </Filename>
        </SubCategoryName>
    </Category>
</Skeleton>
Dimitre Novatchev
hello, I've tried this but I get CartoET-ET-R23V-01.XML<SKELETON/>...
michael bullock
I was hoping to get the actual xml structure you have described in your results..am I doing something wrong?
michael bullock
@michael-bullock: You'll get such result if you have `<xsl:output method="text/>` or if you issue `<xsl:value-of>` against the result. This is the string value of the result. You should treat the result as markup.
Dimitre Novatchev
Hi Dimitre, sorry I'm not sure I understand. I want to convert my xml file to the xml file you showed in your results. So that I can navigate the nodes in the new way i.e Category, then child node SubCategoryName etc. I'm hoping the xsl will convert the node structure so I can navigate the new child nodes. What I've currentlty done is taken your xsl code saved it as a .xsl file. I then linked xml file contaiing the original xml structure, with the xsl file. When I open the xml file i see 1 line of text which are the values concatenated??
michael bullock
@michael-bullock: Now I don't understand. What do you mean by ""linked xml file contaiing the original xml structure, with the xsl file" ? I don't think IE can perform nested transformations in this way.
Dimitre Novatchev
By link, I mean I have an xml file with the original xml structure. When I open this in IE is see the exact original xml node structure and values as I described in my question. I then took your xsl code , saved it to a .xsl file and then made a reference/declare to the xsl file in my original xml file. When I open back my xml file in IE, i see one line of concatenated values. I was hoping to see the new node structure and values as your results show?? am I doing something wrong or is xsl only deal with values and not output the xmnl nodes/elements??
michael bullock
@michael-bullock: I cannot repro your problem. I updated my answer to show how to produce the transformation results with IE.
Dimitre Novatchev
Hello I think the point I'm trying to make is no examples here show the result that I'm after. I want to be able to use xsl and open the xml file to see the new structure i.e "<SKELETON><CATEGORY>CarTo<SUBCATEGORYNAME>ET-ET-RS23<FILENAME>V.01.XML<XmlDefinition><Skeleton></XmlDefinition></FILENAME></SUBCATEGORY></CATEGORY></SKELETON> instead the xml shows....."CarToET-ET-RS23V.01.xmlSkeleton"
michael bullock
@michael-bullock: AFAIK, IE doesn't process the result of an XML transformation a second time. To see the result as XML displayed by IE you'll need to use Javascript and the MSXML3 DOM.
Dimitre Novatchev
@michael-bullock: When you right-click on the IE display and select "view source", you'll see the result of the transformation.
Dimitre Novatchev
Thanks for being patient I understand now. It is possible you extend your xsl example ignore/to not write out the <xmldefinition> and <filename> nodes please?
michael bullock
@michael-bullock: What do you mean? Please, edit your question with your new desired result and explain that this is a change from the original question.
Dimitre Novatchev