473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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='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!
Jul 20 '05 #1
3 1771


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: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
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
chris yoker <cy****@hotmail.com> 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

Jul 20 '05 #3
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!
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
1
by: Patrick Reilly | last post by:
I am trying to implement a better method than I already have to document database schemas with XML, and use XSLT to both generate database DDL statements (CREATE TABLE, etc) and to transform to...
3
by: Mike Whittemore | last post by:
I am trying to convert an HTML table into a list of name-value pairs, one pair per field in the table. I believe my XSLT is correct, but I've tried both Xalan and Saxon, which both fail with...
0
by: shaun | last post by:
Question about :what is a reasonable time for a transform? I am transforming an XML file of ~70 000 lines into a file of ~100 000 lines using XSLT, and also doing the reverse to reconstitute the...
1
by: Wil | last post by:
I'm very new to developing in .NET and even newer to XML. The past few days have been pretty frustrating for me because I'm trying to perform a transform on data in a dataset and it's not working....
4
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
0
by: krina | last post by:
I am new in this field and have just started learning XML and xslt, Now I am having a XML file.......having structure as follows..... <?xml version="1.0" encoding="UTF-8"?> <page> ...
1
by: MaggotChild | last post by:
Hi, I'm trying to transform the values entered into a form: HTML: <div id="container"> <div id="bs"> <input id="name" type="text"/> <p>PPPPP</p> <a href="#" onclick="return transform()">Do...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.