Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: how to transform the actual XML structure

Reply
Thread Tools

XSLT: how to transform the actual XML structure

 
 
chris yoker
Guest
Posts: n/a
 
      01-18-2005
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!
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-18-2005


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

<xslutput 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/
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      01-18-2005
chris yoker <(E-Mail Removed)> 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

 
Reply With Quote
 
chris yoker
Guest
Posts: n/a
 
      01-18-2005
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to: Transform a XML structure to a string element value Riaan XML 1 06-10-2008 11:55 AM
Re: Help - how to preserve XML Structure using C++ XSL Transform? Joe Fawcett C++ 0 11-21-2007 10:43 AM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
What am I missing re: very simple XML->XML transform Duane Morin XML 3 01-26-2004 05:44 PM
What parses the xml/xsl transform in asp.net? System.xml NOT msxml? KathyB ASP .Net 0 06-25-2003 05:03 PM



Advertisments