XSLT: how to transform the actual XML structure

Discussion in 'XML' started by chris yoker, Jan 18, 2005.

  1. chris yoker

    chris yoker Guest

    Hiya,

    I need to convert the actual markup in this XML, so that I can databind.

    I need to convert it:
    FROM:

    <rows>
    <row>
    <FIELD NAME="PRODUCT-TYPE">CAR</FIELD>
    <FIELD NAME="PRODUCT-DATE">01/01/2004</FIELD
    </row>
    <row>
    <FIELD NAME="PRODUCT-TYPE">BIKE</FIELD>
    <FIELD NAME="PRODUCT-DATE">01/01/2003</FIELD
    </row>
    </rows>


    TO:

    <rows>
    <row>
    <PRODUCT-TYPE>CAR</PRODUCT-TYPE>
    <PRODUCT-DATE>01/01/2004</PRODUCT-DATE>
    </row>
    <row>
    <PRODUCT-TYPE>BIKE</PRODUCT-TYPE>
    <PRODUCT-DATE>01/01/2003</PRODUCT-DATE>
    </row>
    </rows>

    I’m trying with the following XSLT, but obviously, it isn’t giving me
    what I want :-(
    Can anyone give me a push in the right direction?

    Many thanks,
    yogi

    <xsl:template match="*">
    <xsl:when test="@NAME='PRODUCT-TYPE'">
    <xsl:for-each select="FIELD">
    <xsl:choose>
    <xsl:when test="@NAME='PRODUCT-TYPE'">
    <PRODUCT-TYPE>
    <xsl:value-of select="." />
    </PRODUCT-TYPE>
    </xsl:when>
    </xsl:choose>
    </xsl:for-each>
    </xsl:when>
    </xsl:template>



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    chris yoker, Jan 18, 2005
    #1
    1. Advertising

  2. chris yoker wrote:


    > I need to convert it:
    > FROM:
    >
    > <rows>
    > <row>
    > <FIELD NAME="PRODUCT-TYPE">CAR</FIELD>
    > <FIELD NAME="PRODUCT-DATE">01/01/2004</FIELD
    > </row>
    > <row>
    > <FIELD NAME="PRODUCT-TYPE">BIKE</FIELD>
    > <FIELD NAME="PRODUCT-DATE">01/01/2003</FIELD
    > </row>
    > </rows>
    >
    >
    > TO:
    >
    > <rows>
    > <row>
    > <PRODUCT-TYPE>CAR</PRODUCT-TYPE>
    > <PRODUCT-DATE>01/01/2004</PRODUCT-DATE>
    > </row>
    > <row>
    > <PRODUCT-TYPE>BIKE</PRODUCT-TYPE>
    > <PRODUCT-DATE>01/01/2003</PRODUCT-DATE>
    > </row>
    > </rows>


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

    <xsl:eek:utput method="xml" encoding="UTF-8" />

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

    <xsl:template match="FIELD">
    <xsl:element name="{@NAME}">
    <xsl:apply-templates />
    </xsl:element>
    </xsl:template>

    </xsl:stylesheet>

    All you need is the indentity transformation for all nodes besides
    <FIELD> elements where a template is needed to create a new element with
    the name of the attribute NAME.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 18, 2005
    #2
    1. Advertising

  3. chris yoker <> writes:

    > Hiya,
    >
    > I need to convert the actual markup in this XML, so that I can databind.
    >
    > I need to convert it:
    > FROM:
    >
    > <rows>
    > <row>
    > <FIELD NAME="PRODUCT-TYPE">CAR</FIELD>
    > <FIELD NAME="PRODUCT-DATE">01/01/2004</FIELD
    > </row>
    > <row>
    > <FIELD NAME="PRODUCT-TYPE">BIKE</FIELD>
    > <FIELD NAME="PRODUCT-DATE">01/01/2003</FIELD
    > </row>
    > </rows>
    >
    >
    > TO:
    >
    > <rows>
    > <row>
    > <PRODUCT-TYPE>CAR</PRODUCT-TYPE>
    > <PRODUCT-DATE>01/01/2004</PRODUCT-DATE>
    > </row>
    > <row>
    > <PRODUCT-TYPE>BIKE</PRODUCT-TYPE>
    > <PRODUCT-DATE>01/01/2003</PRODUCT-DATE>
    > </row>
    > </rows>
    >
    > I’m trying with the following XSLT, but obviously, it isn’t giving me
    > what I want :-(
    > Can anyone give me a push in the right direction?
    >
    > Many thanks,
    > yogi
    >
    > <xsl:template match="*">

    xsl:when has to be inside xsl:choose so the following line will stop
    your stylesheet compiling with a syntax error, but probably matching on
    * then using a big xsl:choose is the wrong approach

    > <xsl:when test="@NAME='PRODUCT-TYPE'">
    > <xsl:for-each select="FIELD">
    > <xsl:choose>
    > <xsl:when test="@NAME='PRODUCT-TYPE'">
    > <PRODUCT-TYPE>
    > <xsl:value-of select="." />
    > </PRODUCT-TYPE>
    > </xsl:when>
    > </xsl:choose>
    > </xsl:for-each>
    > </xsl:when>
    > </xsl:template>
    >


    start with the identity transform:

    <xsl:template match="*">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    Then add a template for FIELD that does something different:

    <xsl:template match="FIELD">
    <xsl:element name="{@NAME}">
    <xsl:value-of select="."/>
    </xsl:element>
    </xsl:template>

    and that's all you should need.

    David
    David Carlisle, Jan 18, 2005
    #3
  4. chris yoker

    chris yoker Guest

    Hi David and Martin.

    I can't pretend to fully understand, but it worked.
    Many thanks for the elegant XSLT :)

    yogi

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    chris yoker, Jan 18, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Isambella  via DotNetMonster.com

    Transform XML string using XSLT file

    Isambella via DotNetMonster.com, Aug 1, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    9,723
    Isambella via DotNetMonster.com
    Aug 2, 2005
  2. Igor
    Replies:
    1
    Views:
    4,960
    Dimitre Novatchev
    Jul 20, 2003
  3. Replies:
    0
    Views:
    491
  4. Replies:
    1
    Views:
    663
    Joris Gillis
    Feb 2, 2005
  5. adi
    Replies:
    1
    Views:
    31,582
    Joe Kesselman
    Jun 6, 2006
Loading...

Share This Page