473,410 Members | 1,930 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,410 software developers and data experts.

XSLT help

Hey everyone, I'm having some trouble with this. I need to convert one
xml doc into another and I think that XSLT may be the answer, so I'd
like some help from the gurus out here :) If XSLT cannot solve the
problem then I think I'll have to write a Java program using DOM or
something.

Here is an example of what I am trying to accomplish - basically, I
need to do a lookup on the author's first name and last name and then
create the nested XML structure containing book info by that author

Source XML:

<books>
<author>
<fname>William</fname>
<lname>Shakespeare</lame>
<bookName>Merchant of Venice</bookName>
<bookRank>One</bookRank>
</author>
<author>
<fname>William</fname>
<lname>Shakespeare</lame>
<bookName>As You Like It</bookName>
<bookRank>Two</bookRank>
</author>
</books>

Needs to be transformed to:

<books>
<author>
<fname>William</fname>
<lname>Shakespeare</lname>
<books>
<book>
<bookName>Merchant of Venice</bookName>
<bookRank>One</bookRank>
</book>
<book>
<bookName>As You Like It</bookName>
<bookRank>Two</bookRank>
</book>
</books>
</author>
</books>

I'm not an expert in XSL so don't know if this is even possible?
Thanks,
Sabrina

Jan 28 '07 #1
3 2328
si************@hotmail.com wrote:
Hey everyone, I'm having some trouble with this. I need to convert one
xml doc into another and I think that XSLT may be the answer, so I'd
like some help from the gurus out here :) If XSLT cannot solve the
problem then I think I'll have to write a Java program using DOM or
something.

Here is an example of what I am trying to accomplish - basically, I
need to do a lookup on the author's first name and last name and then
create the nested XML structure containing book info by that author

Source XML:

<books>
<author>
<fname>William</fname>
<lname>Shakespeare</lame>
<bookName>Merchant of Venice</bookName>
<bookRank>One</bookRank>
</author>
<author>
<fname>William</fname>
<lname>Shakespeare</lame>
<bookName>As You Like It</bookName>
<bookRank>Two</bookRank>
</author>
</books>

Needs to be transformed to:

<books>
<author>
<fname>William</fname>
<lname>Shakespeare</lname>
<books>
<book>
<bookName>Merchant of Venice</bookName>
<bookRank>One</bookRank>
</book>
<book>
<bookName>As You Like It</bookName>
<bookRank>Two</bookRank>
</book>
</books>
</author>
</books>

I'm not an expert in XSL so don't know if this is even possible?
Thanks,
Sabrina
in xslt2, this does what you need, he xsl1 version would be similar just
replace xsl:for-each-group by the muenchian grouping technique (ask google)

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="books">
<books>
<xsl:for-each-group select="author"
group-by="concat(fname,'/',lname)">
<author>
<xsl:copy-of select="fname,lname"/>
<books>
<xsl:for-each select="current-group()">
<book>
<xsl:copy-of select="bookName,bookRank"/>
</book>
</xsl:for-each>
</books>
</author>
</xsl:for-each-group>
</books>
</xsl:template>
</xsl:stylesheet>

Jan 28 '07 #2
David Carlisle wrote:
si************@hotmail.com wrote:
replace xsl:for-each-group by the muenchian grouping technique (ask google)
Or, more directly, see the XSLT FAQ for examples of that approach:
http://www.dpawson.co.uk/xsl/sect2/sect21.html

See the Grouping page, and possibly the Sorting and Grouping page as well.

(If you're working in XSLT, Dave Pawson's collection of tips gathered
from the XSLT Users' mailing list is a *wonderful* resource -- it
summarizes and organizes years of experience in making XSLT do things
ranging from simple to "I thought that was impossible.")

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jan 28 '07 #3
That is just beautiful! I know about the muenchian grouping technique
but I think its such a pain to use. Its great that XSL2.0 has this
feature.

FYI, I'm using SAXON as it supports xsl 2.0.

Sample Java Code:

public static void main(String[] args) throws Exception
{
System.setProperty("javax.xml.transform.Transforme rFactory",
"net.sf.saxon.TransformerFactoryImpl");

String xslID = "transform.xsl" ;
String sourceID = "file.xml";

// Create a transform factory instance.
TransformerFactory tfactory =
TransformerFactory.newInstance();
InputStream xslIS =
new BufferedInputStream(new FileInputStream(xslID));
StreamSource xslSource = new StreamSource(xslIS);
// Create a transformer for the stylesheet.
Transformer transformer = tfactory.newTransformer(xslSource);
InputStream xmlIS =
new BufferedInputStream(new FileInputStream(sourceID));
StreamSource xmlSource = new StreamSource(xmlIS);

// Transform the source XML to System.out.
transformer.transform(xmlSource, new
StreamResult(System.out));
}

Jan 28 '07 #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: Victor | last post by:
Hi, this might sound silly but I cannot figure out how to incorporate some XML of employees into some XML of a company to give new XML containing them all. For example, this is the company now ...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
5
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating...
4
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
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...
15
by: Jeff Uchtman | last post by:
Can I draw from 2 XML sources, the structure is exactly the same execpt for data contained into 1 xslt using math to add some structrure, and displaying others as node 1 and node 2? This data is...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.