Transform XML using XSLT


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Transform XML using XSLT

  1. #1
    Join Date
    Dec 2003
    Location
    SouthWest England
    Posts
    21

    Transform XML using XSLT

    I'm a complete XML newbie and I'm attempting to use an XSLT to transform an
    string containing XML so that the nodes are named as per my requirements and
    nodes that I don't need arem't written.

    Now the renaming of the nodes works but I'm not sure how to not copy the
    nodes that I don't want.

    Here's the code I'm using to do the existing transformation (posted by Oleg
    Tkachenko on the microsoft.public.dotnet.xml ) which as I said works great and I'm sure that all that is
    required is a
    change to the xsltherwise section but what is the best way to go about it?
    should I try another xsl:choose ,xsl:if or something else?

    Any help greatly appreciated.

    Nathan

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

    <xsl:template match="*">
    <xsl:variable name="rule"
    select="document('mapping.xml')/mapping/rule[@inbound=name(current())]"/>
    <xsl:choose>
    <xsl:when test="$rule">
    <xsl:element name="{$rule/@outbound}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:element>
    </xsl:when>
    <xsltherwise>
    <!-- No mapping - copy as is -->
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsltherwise>
    </xsl:choose>

    </xsl:template>


    </xsl:stylesheet>

  2. #2
    Join Date
    Dec 2003
    Location
    SouthWest England
    Posts
    21
    Problem solved. The answer is to remove the copy from the otherwise instruction, this then only copies the nodes that are listed in the mapping document.

    Nathan

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

    <xsl:template match="*">
    <xsl:variable name="rule"
    select="document('mapping.xml')/mapping/rule[@inbound=name(current())]"/>
    <xsl:choose>
    <xsl:when test="$rule">
    <xsl:element name="{$rule/@outbound}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:element>
    </xsl:when>
    <xsltherwise>
    <xsl:apply-templates/>
    </xsltherwise>
    </xsl:choose>

    </xsl:template>


    </xsl:stylesheet>

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