XSLT: how to transform the actual XML structure

C

chris yoker

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>
 
M

Martin Honnen

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.
 
D

David Carlisle

chris yoker said:
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
 
C

chris yoker

Hi David and Martin.

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

yogi
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top