473,382 Members | 1,204 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.

Tranforming the format of XML

Hello, please can someone help me with the following:

If I have the following XML.

<Tables>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>UserId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>PersonId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>Notes</COLUMN_NAME>
<DATA_TYPE>varchar</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>Person</TABLE_NAME>
<COLUMN_NAME>PersonId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
</Tables>

What is the best/easiest way to transform it into the following format in
C#/XSL:

<Tables>
<Table Name="User">
<COLUMN DataType="bigint">UserId</COLUMN>
<COLUMN DataType="bigint">PersonId</COLUMN>
<COLUMN DataType="varchar">Notes</COLUMN>
</Table>
<Table Name="Person">
<COLUMN DataType="bigint">PersonId</COLUMN>
</Table>
</Tables>
Nov 12 '05 #1
4 1109
suzy wrote:
If I have the following XML.

<Tables>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>UserId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>PersonId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>Notes</COLUMN_NAME>
<DATA_TYPE>varchar</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>Person</TABLE_NAME>
<COLUMN_NAME>PersonId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
</Tables>

What is the best/easiest way to transform it into the following format in
C#/XSL:

<Tables>
<Table Name="User">
<COLUMN DataType="bigint">UserId</COLUMN>
<COLUMN DataType="bigint">PersonId</COLUMN>
<COLUMN DataType="varchar">Notes</COLUMN>
</Table>
<Table Name="Person">
<COLUMN DataType="bigint">PersonId</COLUMN>
</Table>
</Tables>


Muenchian grouping method is your friend:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="tableKey" match="Table" use="TABLE_NAME"/>
<xsl:template match="Tables">
<Tables>
<xsl:for-each select="Table[count(.|key('tableKey',TABLE_NAME)[1])=1]">
<table name="{TABLE_NAME}">
<xsl:apply-templates select="key('tableKey',TABLE_NAME)"/>
</table>
</xsl:for-each>
</Tables>
</xsl:template>
<xsl:template match="Table">
<COLUMN DataType="{DATA_TYPE}">
<xsl:value-of select="COLUMN_NAME"/>
</COLUMN>
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #2
Hi,

Thanks for your help. I am not really an XSL expert so I don't really
understand what's going on there. But it's nearly right, but not quite
right because my output is:

UserIdPersonIdNotes
PersonId

I am missing the node/attribute text. Any ideas?
"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OW**************@TK2MSFTNGP10.phx.gbl...
suzy wrote:
If I have the following XML.

<Tables>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>UserId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>PersonId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>User</TABLE_NAME>
<COLUMN_NAME>Notes</COLUMN_NAME>
<DATA_TYPE>varchar</DATA_TYPE>
</Table>
<Table>
<TABLE_NAME>Person</TABLE_NAME>
<COLUMN_NAME>PersonId</COLUMN_NAME>
<DATA_TYPE>bigint</DATA_TYPE>
</Table>
</Tables>

What is the best/easiest way to transform it into the following format in C#/XSL:

<Tables>
<Table Name="User">
<COLUMN DataType="bigint">UserId</COLUMN>
<COLUMN DataType="bigint">PersonId</COLUMN>
<COLUMN DataType="varchar">Notes</COLUMN>
</Table>
<Table Name="Person">
<COLUMN DataType="bigint">PersonId</COLUMN>
</Table>
</Tables>


Muenchian grouping method is your friend:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="tableKey" match="Table" use="TABLE_NAME"/>
<xsl:template match="Tables">
<Tables>
<xsl:for-each select="Table[count(.|key('tableKey',TABLE_NAME)[1])=1]">
<table name="{TABLE_NAME}">
<xsl:apply-templates select="key('tableKey',TABLE_NAME)"/>
</table>
</xsl:for-each>
</Tables>
</xsl:template>
<xsl:template match="Table">
<COLUMN DataType="{DATA_TYPE}">
<xsl:value-of select="COLUMN_NAME"/>
</COLUMN>
</xsl:template>
</xsl:stylesheet>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3
suzy wrote:
Thanks for your help. I am not really an XSL expert so I don't really
understand what's going on there. Read excellent Jeni Tennison's explanation of this method at
http://www.jenitennison.com/xslt/gro...muenchian.html
But it's nearly right, but not quite
right because my output is:

UserIdPersonIdNotes
PersonId
Hmmm, what is this? Looks like you are browsing result of the
transformation in IE?
I am missing the node/attribute text. Any ideas?

That's how IE shows XML when it thinks it's HTML - all non HTML tags are
omitted along with their attributes.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #4
:)

Yes you were correct! It works perfectly if I transform it in .NET code -
thanks very much ! :)

Suzy

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
suzy wrote:
Thanks for your help. I am not really an XSL expert so I don't really
understand what's going on there.

Read excellent Jeni Tennison's explanation of this method at
http://www.jenitennison.com/xslt/gro...muenchian.html
But it's nearly right, but not quite
right because my output is:

UserIdPersonIdNotes
PersonId


Hmmm, what is this? Looks like you are browsing result of the
transformation in IE?
I am missing the node/attribute text. Any ideas?

That's how IE shows XML when it thinks it's HTML - all non HTML tags are
omitted along with their attributes.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #5

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

Similar topics

3
by: Lowell Kirsh | last post by:
In Peter Norvig's Infrequently Answered Questions he explains that the following 2 fnctions look almost identical but are not the same: def printf(format, *args): print format % args, def...
15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
3
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
6
by: Dario Di Bella | last post by:
Hi all, we have the following urgent issue affecting our development team. Initially we had one particular workstation that failed executing queries on a DB2 database, raising an invalid date...
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
4
by: suzy | last post by:
Hello, please can someone help me with the following: If I have the following XML. <Tables> <Table> <TABLE_NAME>User</TABLE_NAME> <COLUMN_NAME>UserId</COLUMN_NAME>...
4
by: David Morris | last post by:
Hi Could somebody please explain what the following line of code means String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1 It's actually the first argument that I don't understand. What is...
13
by: Roy | last post by:
Hi all, I'm creating a project that should always use this date format when displays the dates or create dates. The back end database is a SQL Server and I like to know what is the logical way...
3
by: Carl Trachte | last post by:
Hello. Python 3.0.a1 has been released. I'm trying to get the hang of the new string formatting in the form: '5.66' There are more options in PEP 3101 (fill, alignment, etc.), but I'm having...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: 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.