473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2337
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
2719
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
8664
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
3193
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
7604
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
1817
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
2140
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
2825
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
1996
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
2042
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
2135
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
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
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
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
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
4573
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...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.