开发者

Modify XML structure to create a Nested Tree XML structure using XSD or XSLT

开发者 https://www.devze.com 2023-01-19 00:25 出处:网络
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

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 expr开发者_StackOverflowession 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


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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>


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号