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

Help needed with Transpose XML and XSLT problem

Hi,
I am coming across problems in trying to EFFICIENTLY merge to XML
files into one which involves transposing the rows into columns so that
I can either generate a single flat xml file or store it in a flat
table in the database.

The 2 XML files which I have are:

<xml>
<keyword>
<name>abc</name>
<count>143</count>
</keyword>
<keyword>
<name>def</name>
<count>757</count>
</keyword>
<keyword>
<name>ghi</name>
<count>221</count>
</keyword>
</xml>
<xml>
<response searchTerm="abc">
<listing rank="1" bid="0.5"/>
<listing rank="2" bid="6.5"/>
<listing rank="3" bid="4.5"/>
</response>
<response searchTerm="def">
<listing rank="1" bid="4.5"/>
<listing rank="2" bid="34.5"/>
<listing rank="3" bid="1.5"/>
</response>
<response searchTerm="ghi">
<listing rank="1" bid="33.5"/>
<listing rank="2" bid="1.5"/>
<listing rank="3" bid="6.5"/>
</response>
</xml>

I want to merge the 2 xml files into one XML file and later want to
store it into a flat database table.

The structure of the output xml file is:

<xml>
<row keyword="abc" count="143" bid1="0.5" bid2="6.5"
bid3="4.5"/>
<row keyword="def" count="757" bid1="4.5" bid2="34.5"
bid3="1.5"/>
<row keyword="ghi" count="221" bid1="33.5" bid2="1.5"
bid3="6.5"/>
</xml>

Similariy the database table columns will be:
autoid, keyword, count, bid1, bid2, bid3

I am able to achieve this join by parsing the xml file, but I want to
do it as efficiently as possible (I guess using XSLT) as I have to do
this for 10,000+ thousand keywords.

Any help is greatly appreciated.

Thanks,

Amber

Jan 13 '06 #1
4 2106
Hi Amber,
I merged the files in this source

<?xml version="1.0"?>
<xml>
<keyword>
<name>abc</name>
<count>143</count>
</keyword>
<keyword>
<name>def</name>
<count>757</count>
</keyword>
<keyword>
<name>ghi</name>
<count>221</count>
</keyword>
<response searchTerm="abc">
<listing rank="1" bid="0.5"/>
<listing rank="2" bid="6.5"/>
<listing rank="3" bid="4.5"/>
</response>
<response searchTerm="def">
<listing rank="1" bid="4.5"/>
<listing rank="2" bid="34.5"/>
<listing rank="3" bid="1.5"/>
</response>
<response searchTerm="ghi">
<listing rank="1" bid="33.5"/>
<listing rank="2" bid="1.5"/>
<listing rank="3" bid="6.5"/>
</response>
</xml>

after that, I transformed it with the stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:apply-templates select="xml"/>
</xsl:template>

<xsl:template match="xml">
<xml>
<xsl:for-each select="./keyword">
<row>
<xsl:attribute name="keyword">
<xsl:value-of select="./name"/>
</xsl:attribute>
<xsl:attribute name="count">
<xsl:value-of select="./count"/>
</xsl:attribute>
<xsl:attribute name="bid1">
<xsl:value-of
select="//response[@searchTerm=current()/name]/listing[@rank='1']/@bid"/>
</xsl:attribute>
<xsl:attribute name="bid2">
<xsl:value-of
select="//response[@searchTerm=current()/name]/listing[@rank='2']/@bid"/>
</xsl:attribute>
<xsl:attribute name="bid3">
<xsl:value-of
select="//response[@searchTerm=current()/name]/listing[@rank='3']/@bid"/>
</xsl:attribute>
</row>
</xsl:for-each>
</xml>
</xsl:template>
</xsl:stylesheet>

it produces the output

<?xml version="1.0"?>
<xml>
<row keyword="abc" count="143" bid1="0.5" bid2="6.5" bid3="4.5"/>
<row keyword="def" count="757" bid1="4.5" bid2="34.5" bid3="1.5"/>
<row keyword="ghi" count="221" bid1="33.5" bid2="1.5" bid3="6.5"/>
</xml>

I tested it into Stylus Studio 2006.

I don't know how to merge two files with XSLT efficiently.
I tried this solution (to me it seems to be a silly way to merge files)

first i created two xml files:

doc1.xml contains the keywords
doc2.xml contains the response

doc1:

<?xml version="1.0"?>
<xml>
<keyword>
<name>abc</name>
<count>143</count>
</keyword>
<keyword>
<name>def</name>
<count>757</count>
</keyword>
<keyword>
<name>ghi</name>
<count>221</count>
</keyword>
</xml>

doc2:

<?xml version="1.0"?>
<xml>
<response searchTerm="abc">
<listing rank="1" bid="0.5"/>
<listing rank="2" bid="6.5"/>
<listing rank="3" bid="4.5"/>
</response>
<response searchTerm="def">
<listing rank="1" bid="4.5"/>
<listing rank="2" bid="34.5"/>
<listing rank="3" bid="1.5"/>
</response>
<response searchTerm="ghi">
<listing rank="1" bid="33.5"/>
<listing rank="2" bid="1.5"/>
<listing rank="3" bid="6.5"/>
</response>
</xml>

now the main xml file:

<?xml version="1.0"?>
<docs>
<file href="doc1.xml"/>
<file href="doc2.xml"/>
</docs>

on this file must be applyed the stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="keyword-file" select="document(//file[1]/@href)"/>
<xsl:variable name="response-file"
select="document(//file[2]/@href)"/>

<xsl:template match="/">
<xml>
<xsl:apply-templates select="$keyword-file/xml" mode="1"/>
<xsl:apply-templates select="$response-file/xml" mode="2"/>
</xml>
</xsl:template>

<xsl:template match="xml" mode="1">
<xsl:for-each select="keyword">
<keyword>
<name>
<xsl:value-of select="./name"/>
</name>
<count>
<xsl:value-of select="./count"/>
</count>
</keyword>
</xsl:for-each>
</xsl:template>

<xsl:template match="xml" mode="2">
<xsl:for-each select="response">
<response>
<xsl:attribute name="searchTerm">
<xsl:value-of select="./@searchTerm"/>
</xsl:attribute>
<xsl:for-each select="listing">
<listing>
<xsl:attribute name="rank">
<xsl:value-of select="./@rank"/>
</xsl:attribute>
<xsl:attribute name="bid">
<xsl:value-of select="./@bid"/>
</xsl:attribute>
</listing>
</xsl:for-each>
</response>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

It works with Stylus Studio 2006 built-in parser.
I hope it can be useful.

Please, can anybody tell me a better way to merge files?
Thanks in advance,

Monique

Jan 15 '06 #2
I just tried it Monique and it works as expected. Thanks :).
I have one more question though, is there a way to generalize the XSLT
to generate the output xml for a variable nummber of bids. e.g. can we
have the xslt to work for 1-10 listings instead of the 1-3 listings it
works for now. I see that you have hardcoded the bid/rank values there.
Its not a big problem though, I can do without it.
Also, can you point me to some good tutorials for learning xslt.

Thanks,

Amber

Jan 16 '06 #3
Hi Amber,

These are my favourite tutorials:

http://www.zvon.org/xxl/XSLTutorial/Output/index.html
http://www.w3schools.com/default.asp

I'm trying to remove the hardcoding, any help from the list is
appreciated :-)

Bye

Monique

Jan 16 '06 #4
Hi,

here it is:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="xml" />
</xsl:template>
<xsl:variable name="basename" select='bid' />
<xsl:template match="xml">
<xml>
<xsl:for-each select="./keyword">
<row>
<xsl:attribute name="keyword">
<xsl:value-of select="./name" />
</xsl:attribute>
<xsl:attribute name="count">
<xsl:value-of select="./count" />
</xsl:attribute>
<xsl:variable name="node"
select="//response[@searchTerm=current()/name]" />
<xsl:for-each select="$node/listing">
<xsl:variable name="rank" select="@rank" />
<xsl:attribute name="bid{$rank}">
<xsl:value-of select="@bid" />
</xsl:attribute>
</xsl:for-each>
</row>
</xsl:for-each>
</xml>
</xsl:template>
</xsl:stylesheet>

Bye

Monique

Jan 16 '06 #5

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

Similar topics

4
by: Ringo Langly | last post by:
Hi all, I'm a seasoned web programmer, but I've never touched XSLT. It's always been one of those acronyms I've never needed to educate myself on. Now... we're working with a web content...
2
by: A. Wiebenga | last post by:
Hi all! I am currently involved in a project in which I am supposed to write a XSLT-transformation sheet for some XML data. I will outline the situation first: I've got one large XML file...
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]<--
22
by: Rafia Tapia | last post by:
Hi all This is what I have in mind and I will appreciate any suggestions. I am trying to create a xml help system for my application. The schema of the xml file will be <helpsystem> <help...
1
by: Torrent | last post by:
The Following is an Error I am getting using VS.NET 2003. When I bind the XML and XSLT Document together in Internet Explorer it works fine. However when I attempt to Load the File with the...
3
by: Torrent | last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown...
7
by: Roshawn Dawson | last post by:
Hi, I have an xslt file located in the root directory. It is used by an aspx pages in both the root directory and a subdirectory. But for some strange reason, the aspx page in the subdirectory...
6
Gaiason
by: Gaiason | last post by:
Hi XML/XSLT masters and gurus, I am a newbie in XML/XSLT and have been reading up XML/XSLT in order to convert a XML generated from a OCR engine to another format of XML. I am truly stuck at this...
1
by: eskelies | last post by:
Below you will find the code that I have written. I am asking it to search for an Excel file. Once it finds the correct dated Excel file it opens it. From there it is in search of information. I...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.