DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Modifying XMl element names using XSLT

  1. #1
    Vijay Guest

    Modifying XMl element names using XSLT



    Hi,

    I need to change the node names in my XML file based on a condition.
    If a an element has children then the element name has to become
    "folder",
    and if an element has no children then its name has to become "leaf".
    For example :

    <Employees>
    <employee name="emp5" id="5">
    <employee name="emp2" id="2">
    <employee name="emp1" id="1"/>
    <employee name="emp4" id="4"/>
    </employee>
    <employee name="emp3" id="3"/>
    </employee>
    </Employees>


    As :

    <Employees>
    <folder name="emp5" id="5">
    <folder name="emp2" id="2">
    <leaf name="emp1" id="1"/>
    <leaf name="emp4" id="4"/>
    </folder>
    <leaf name="emp3" id="3"/>
    </folder>
    </Employees>

    I wanted to use XSLT. Do you have a hint how can I achieve that ?

    Thanks
    vijay


  2. #2
    Russell Jones Guest

    Re: Modifying XMl element names using XSLT

    Something like this should work:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xslutput method="xml" encoding="UTF-8"/>
    <xsl:template match="/">
    <xsl:apply-templates select="Employees" />
    </xsl:template>

    <xsl:template match="Employees">
    <Employees><xsl:apply-templates select="employee"/></Employees>
    </xsl:template>

    <xsl:template match="employee[count(child::*) &gt; 0]">
    <folder><xsl:apply-templates select="@* | node()"/></folder>
    </xsl:template>

    <xsl:template match="employee[count(child::*) = 0]">
    <leaf><xsl:apply-templates select="@* |

    node()"/><xsl:apply-templates select="employee"/></leaf>
    </xsl:template>

    <xsl:template match="@* | node()">
    <xsl:copy />
    </xsl:template>

    </xsl:stylesheet>

    "Vijay" <mvbabu@hotmail.com> wrote in message
    news:3db97c45$1@tnews.web.devx.com...
    >
    >
    > Hi,
    >
    > I need to change the node names in my XML file based on a condition.
    > If a an element has children then the element name has to become
    > "folder",
    > and if an element has no children then its name has to become "leaf".
    > For example :
    >
    > <Employees>
    > <employee name="emp5" id="5">
    > <employee name="emp2" id="2">
    > <employee name="emp1" id="1"/>
    > <employee name="emp4" id="4"/>
    > </employee>
    > <employee name="emp3" id="3"/>
    > </employee>
    > </Employees>
    >
    >
    > As :
    >
    > <Employees>
    > <folder name="emp5" id="5">
    > <folder name="emp2" id="2">
    > <leaf name="emp1" id="1"/>
    > <leaf name="emp4" id="4"/>
    > </folder>
    > <leaf name="emp3" id="3"/>
    > </folder>
    > </Employees>
    >
    > I wanted to use XSLT. Do you have a hint how can I achieve that ?
    >
    > Thanks
    > vijay
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center