Tranforming the format of XML

  • Thread starter Thread starter suzy
  • Start date Start date
S

suzy

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>
 
suzy said:
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>
 
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 said:
suzy said:
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>
 
suzy said:
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/grouping/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.
 
:)

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

Suzy
 
Back
Top