473,408 Members | 1,854 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,408 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 15 '05 #1
4 1044
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 15 '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 15 '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 15 '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 15 '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
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>...
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...
3
by: Melissa | last post by:
What specifically causes the Format event of a report's section to fire? Thanks! Melissa
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: 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
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...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
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.