XSLT: how to transform the actual XML structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris yoker

    XSLT: how to transform the actual XML structure

    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='PR ODUCT-TYPE'">
    <xsl:for-each select="FIELD">
    <xsl:choose>
    <xsl:when test="@NAME='PR ODUCT-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!
  • Martin Honnen

    #2
    Re: XSLT: how to transform the actual XML structure



    chris yoker wrote:

    [color=blue]
    > 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>[/color]

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

    <xsl:output 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

    Comment

    • David Carlisle

      #3
      Re: XSLT: how to transform the actual XML structure

      chris yoker <cyoker@hotmail .com> writes:
      [color=blue]
      > 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="*">[/color]
      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
      [color=blue]
      > <xsl:when test="@NAME='PR ODUCT-TYPE'">
      > <xsl:for-each select="FIELD">
      > <xsl:choose>
      > <xsl:when test="@NAME='PR ODUCT-TYPE'">
      > <PRODUCT-TYPE>
      > <xsl:value-of select="." />
      > </PRODUCT-TYPE>
      > </xsl:when>
      > </xsl:choose>
      > </xsl:for-each>
      > </xsl:when>
      > </xsl:template>
      >[/color]

      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

      Comment

      • chris yoker

        #4
        Re: XSLT: how to transform the actual XML structure

        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!

        Comment

        Working...