.NETGURU
Transforming XML document into required format...
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngxml' list.


Greg Quinn
If I have an XML document from a DataSet that shows all the users in my
database...

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Users>
<Users_Fname>Greg</Users_Fname>
<Users_ID>1</Users_ID>
</Users>
<Users>
<Users_Fname>Bob</Users_Fname>
<Users_ID>2</Users_ID>
</Users>
</NewDataSet>

What would be the easiest way to automatically convert the XML document, or
the Dataset to write an XML document to look like this?

<entity id="e1">
<description>Users</description>
<oncontextmenu></oncontextmenu>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<contents>
<entity id="e2">
<description>Greg</description>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<onClick>displayUser(1)</onClick>
<contents></contents>
</entity>
</contents>
<contents>
<entity id="e3">
<description>Bob</description>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<onClick>displayUser(2)</onClick>
<contents></contents>
</entity>
</contents>
</entity>

Reply to this message...
 
    
Kirk Allen Evans
This is actually a very straight-forward XSLT transformation. The only
difficult conceptual part of it would be generating the ID for each user,
which is handled by a position() function call. Here is the XSLT, the code
to generate the transformation should be available for whatever parser you
are using (.NET, MSXML, Saxon, etc).

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" indent="yes"
omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <entity id="e1">
            <description>Users</description>
            <oncontextmenu></oncontextmenu>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <contents>
                <xsl:apply-templates select="NewDataSet/Users"/>
            </contents>
        </entity>
    </xsl:template>

    <xsl:template match="Users">
        <entity>
            <xsl:attribute name="id">
                <xsl:value-of select="concat('e',position() + 1)"/>
            </xsl:attribute>
            <description>
                <xsl:value-of select="UsersFname"/>
            </description>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <onClick>
                <xsl:text>displayUser(</xsl:text>
                <xsl:value-of select="Users_ID"/>
                <xsl:text>)</xsl:text>
            </onClick>
            <contents></contents>
        </entity>
    </xsl:template>
</xsl:stylesheet>

Kirk Allen Evans
http://www.xmlandasp.net
"XML and ASP.NET", New Riders Publishing
http://www.amazon.com/exec/obidos/ASIN/073571200X

[Original message clipped]

Reply to this message...
 
    
Greg Quinn
Hi,

Thanks for helping me transform the XML document into my required format, I
tried the .xsl file you gave me but when I check the outputted XML, all I
get is...

<entity id="e1">
<description>Users</description>
<oncontextmenu>
</oncontextmenu>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<contents>
</contents>
</entity>

What is that  stuff from? Also, the XML only displays one <entity>, why
doesn't it display all of them?

Thanks
Greg

-----Original Message-----
From: Kirk Allen Evans [mailto:Click here to reveal e-mail address]
Sent: 20 June 2002 07:39
To: aspngxml
Subject: [aspngxml] RE: Transforming XML document into required
format...

This is actually a very straight-forward XSLT transformation. The only
difficult conceptual part of it would be generating the ID for each user,
which is handled by a position() function call. Here is the XSLT, the code
to generate the transformation should be available for whatever parser you
are using (.NET, MSXML, Saxon, etc).

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" indent="yes"
omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <entity id="e1">
            <description>Users</description>
            <oncontextmenu></oncontextmenu>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <contents>
                <xsl:apply-templates select="NewDataSet/Users"/>
            </contents>
        </entity>
    </xsl:template>

    <xsl:template match="Users">
        <entity>
            <xsl:attribute name="id">
                <xsl:value-of select="concat('e',position() + 1)"/>
            </xsl:attribute>
            <description>
                <xsl:value-of select="UsersFname"/>
            </description>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <onClick>
                <xsl:text>displayUser(</xsl:text>
                <xsl:value-of select="Users_ID"/>
                <xsl:text>)</xsl:text>
            </onClick>
            <contents></contents>
        </entity>
    </xsl:template>
</xsl:stylesheet>

Kirk Allen Evans
http://www.xmlandasp.net
"XML and ASP.NET", New Riders Publishing
http://www.amazon.com/exec/obidos/ASIN/073571200X

[Original message clipped]

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
Kirk Allen Evans
(reposted to aspngxml from private email)

[Original message clipped]

No problem. However, if you have a follow-up question to a post on the
list, post the question to the list. By keeping the discussions on the
lists for everyone to see, you will get much richer feedback.

> I tried the .xsl file you gave me but when I check the outputted XML, all
I
[Original message clipped]

The "" characters you are seeing are called "Byte Order Marks" (BOMs),
and are the result of the transformation process. Specifically, you are
seeing the BOMs as a by-product of an encoding issue. You are likely
encoding the XML one way (UTF-8) which is being saved another (UTF-16).
Post the code that performs the XSLT transformation up to aspngxml so that
we can see where the encoding issue is occurring. How did you come to see
the XML? Did you save it to a file? If so, describe how that file was
saved: the issue may simply be in your manual process.

[Original message clipped]

Post the XML that you are having problems with. Obviously you have changed
something in your input XML, because this works as expected with the
examples you posted earlier. Using the *exact* XML you provided earlier:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
    <Users>
        <Users_Fname>Greg</Users_Fname>
        <Users_ID>1</Users_ID>
    </Users>
    <Users>
        <Users_Fname>Bob</Users_Fname>
        <Users_ID>2</Users_ID>
    </Users>
</NewDataSet>

This transformation produces the following result:

<entity id="e1">
<description>Users</description>
<oncontextmenu></oncontextmenu>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<contents>
<entity id="e2">
<description></description>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<onClick>displayUser(1)</onClick>
<contents></contents>
</entity>
<entity id="e3">
<description></description>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<onClick>displayUser(2)</onClick>
<contents></contents>
</entity>
</contents>
</entity>

Again, this is *exactly* the output that you requested in your post. If
there are additional details about the input XML document that you forgot to
disclose, then feel free to post the stylesheet solution I proposed as well
as your input XML document to one of the XML lists.

Since you are seeing the "contents" element in the output without any child
nodes, the problem is most likely in your XSLT stylesheet in the line:

    <xsl:apply-templates select="NewDataSet/Users"/>

Here, I am using assuming the DataSet is actually named "NewDataSet". This
assumed that the name of the DataSet was not provided as a constructor:

DataSet ds = New DataSet(); //Produces a DataSet named
"MyDataSet"

DataSet myds = New DataSet("SomethingElse");     //Produces a DataSet named
"SomethingElse"

Double-check the name of the DataSet you are having problems with to see if
it really is named "NewDataSet" or if you named it to something else. If
that doesn't fix the problem, make sure that the DataSet actually contains
data. Finally, make sure that the data is in the exact format that you
provided above, case-sensitive and all.

Kirk Allen Evans
http://www.xmlandasp.net
"XML and ASP.NET", New Riders Publishing
http://www.amazon.com/exec/obidos/ASIN/073571200X

Reply to this message...
 
    
Greg Quinn
Thanks for the help Kirk, I forgot I had changed my input XML for ease of
reading for the post!

I just have one more question, if a <USER> has a <CHILD_USER> or a series
thereof, I can't figure out how to transform the <CHILD_USER> to sit under
its parent node, or all the child users to sit under their respective parent
nodes etc, for that matter.

I of course have in my dataset the <PARENT_ID>, but I obviously need to loop
through the dataset and tack all child nodes onto the parents, or something
like that...

i.e

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
    <Users>
        <Users_Fname>Greg</Users_Fname>
        <Users_ID>1</Users_ID>
        <Parent_ID></Parent_ID>
        <Child_Users>
            <Child_Users_Fname>Joe</Child_Users>
            <Child_Users_ID>3</Child_Users>
            <Parent_ID>1</Parent_ID>
            <Child_Users>
                <Child_Users_Fname>Jeeves</Child_Users>
                <Child_Users_ID>8</Child_Users>
                <Parent_ID>3</Parent_ID>
            </Child_Users>
<Child_Users>
    </Users>
    <Users>
        <Users_Fname>Bob</Users_Fname>
        <Users_ID>2</Users_ID>
        <Parent_ID></Parent_ID>
    </Users>
</NewDataSet>

-----Original Message-----
From: Kirk Allen Evans [mailto:Click here to reveal e-mail address]
Sent: 24 June 2002 04:21
To: aspngxml
Subject: [aspngxml] RE: Transforming XML document into required
format...

(reposted to aspngxml from private email)

[Original message clipped]

No problem. However, if you have a follow-up question to a post on the
list, post the question to the list. By keeping the discussions on the
lists for everyone to see, you will get much richer feedback.

> I tried the .xsl file you gave me but when I check the outputted XML, all
I
[Original message clipped]

The "" characters you are seeing are called "Byte Order Marks" (BOMs),
and are the result of the transformation process. Specifically, you are
seeing the BOMs as a by-product of an encoding issue. You are likely
encoding the XML one way (UTF-8) which is being saved another (UTF-16).
Post the code that performs the XSLT transformation up to aspngxml so that
we can see where the encoding issue is occurring. How did you come to see
the XML? Did you save it to a file? If so, describe how that file was
saved: the issue may simply be in your manual process.

[Original message clipped]

Post the XML that you are having problems with. Obviously you have changed
something in your input XML, because this works as expected with the
examples you posted earlier. Using the *exact* XML you provided earlier:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
    <Users>
        <Users_Fname>Greg</Users_Fname>
        <Users_ID>1</Users_ID>
    </Users>
    <Users>
        <Users_Fname>Bob</Users_Fname>
        <Users_ID>2</Users_ID>
    </Users>
</NewDataSet>

This transformation produces the following result:

<entity id="e1">
<description>Users</description>
<oncontextmenu></oncontextmenu>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<contents>
<entity id="e2">
<description></description>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<onClick>displayUser(1)</onClick>
<contents></contents>
</entity>
<entity id="e3">
<description></description>
<image>images/book.gif</image>
<imageOpen>images/bookOpen.gif</imageOpen>
<onClick>displayUser(2)</onClick>
<contents></contents>
</entity>
</contents>
</entity>

Again, this is *exactly* the output that you requested in your post. If
there are additional details about the input XML document that you forgot to
disclose, then feel free to post the stylesheet solution I proposed as well
as your input XML document to one of the XML lists.

Since you are seeing the "contents" element in the output without any child
nodes, the problem is most likely in your XSLT stylesheet in the line:

    <xsl:apply-templates select="NewDataSet/Users"/>

Here, I am using assuming the DataSet is actually named "NewDataSet". This
assumed that the name of the DataSet was not provided as a constructor:

DataSet ds = New DataSet(); //Produces a DataSet named
"MyDataSet"

DataSet myds = New DataSet("SomethingElse");     //Produces a DataSet named
"SomethingElse"

Double-check the name of the DataSet you are having problems with to see if
it really is named "NewDataSet" or if you named it to something else. If
that doesn't fix the problem, make sure that the DataSet actually contains
data. Finally, make sure that the data is in the exact format that you
provided above, case-sensitive and all.

Kirk Allen Evans
http://www.xmlandasp.net
"XML and ASP.NET", New Riders Publishing
http://www.amazon.com/exec/obidos/ASIN/073571200X

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
 
System.Data.DataSet




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification