Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

XML from ADO conversion to new XML via XSL

I am having problems with taking redundant data from a source XML file which
lists each row from a database like so:

<z:row att1="blah" att2="blahh1" .../>
<z:row att1="blah" att2="blahh2" .../>
<z:row att1="blah" att2="blahh3" .../>
<z:row att1="blah2" att2="blahh" .../>
and so on...

Does anyone know how I can use XSL to transfer this into something like the
following?

<rows>
<row att1="blah">
<att2>blahh1</att2>
<att2>blahh2</att2>
<att2>blahh3</att2>
</row>
<row att1="blah2">
...
</row>
...
</rows>
[693 byte] By [Blaznlion] at [2007-11-9 15:27:33]
# 1 Re: XML from ADO conversion to new XML via XSL
This should get you started. If you have a document that looks like this:
<?xml version="1.0"?>
<rows>
<row att1="blah" att2="blahh1"/>
<row att1="blah" att2="blahh2"/>
<row att1="blah" att2="blahh3"/>
<row att1="blah2" att2="blahh"/>
</rows>

You can create this output:
<?xml version="1.0" encoding="UTF-8"?>
<row>
<att1>blah</att1>
<att2>blahh1</att2>
</row>
<row>
<att1>blah</att1>
<att2>blahh2</att2>
</row>
<row>
<att1>blah</att1>
<att2>blahh3</att2>
</row>
<row>
<att1>blah2</att1>
<att2>blahh</att2>
</row>

By using this stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:apply-templates select="rows" />
</xsl:template>

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

<xsl:template match="row">
<row>
<xsl:apply-templates select="@*" />
</row>
</xsl:template>

<xsl:template match="row/@*">
<xsl:variable name="attName" select="local-name(.)"/>
<xsl:element name="{$attName}"><xsl:value-of select="."/></xsl:element>
</xsl:template>

</xsl:stylesheet>

"Blaznlion" <n/a@unauthorized.net> wrote in message
news:3df2f35c$1@tnews.web.dev-archive.com...
>
> I am having problems with taking redundant data from a source XML file
which
> lists each row from a database like so:
>
> <z:row att1="blah" att2="blahh1" .../>
> <z:row att1="blah" att2="blahh2" .../>
> <z:row att1="blah" att2="blahh3" .../>
> <z:row att1="blah2" att2="blahh" .../>
> and so on...
>
> Does anyone know how I can use XSL to transfer this into something like
the
> following?
>
> <rows>
> <row att1="blah">
> <att2>blahh1</att2>
> <att2>blahh2</att2>
> <att2>blahh3</att2>
> </row>
> <row att1="blah2">
> ...
> </row>
> ...
> </rows>
>
>
Russell Jones at 2007-11-11 23:29:45 >
# 2 Re: XML from ADO conversion to new XML via XSL
Here's an XSLT that will do it:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema">
<xsl:output indent="yes" method="xml"/>

<xsl:template match="/">
<Rows>
<xsl:apply-templates select="xml/rs:data/z:row"/>
</Rows>
</xsl:template>

<xsl:template match="xml/rs:data/z:row">
<Row>
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Row>
</xsl:template>

</xsl:stylesheet>
Lou at 2007-11-11 23:30:51 >
# 3 Re: XML from ADO conversion to new XML via XSL
"Lou" <l_kvitek@audiblemagic.com> wrote:
>
>Here's an XSLT that will do it:
>
><xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
> xmlns:rs="urn:schemas-microsoft-com:rowset"
> xmlns:z="#RowsetSchema">
> <xsl:output indent="yes" method="xml"/>
>
> <xsl:template match="/">
> <Rows>
> <xsl:apply-templates select="xml/rs:data/z:row"/>
> </Rows>
> </xsl:template>
>
> <xsl:template match="xml/rs:data/z:row">
> <Row>
> <xsl:for-each select="@*">
> <xsl:element name="{name()}">
> <xsl:value-of select="."/>
> </xsl:element>
> </xsl:for-each>
> </Row>
> </xsl:template>
>
></xsl:stylesheet>

so why adding memo field would produce "An invalid character was found in
text content."

Is there any tips to return memo field with 'rich' character to an xml
file ??
akira at 2007-11-11 23:31:49 >
# 4 Re: XML from ADO conversion to new XML via XSL
The memo field probably contains XML reserved characters such as ampersands.
You need to escape those characters before adding them to the XML document.
You may want to put such "free" text into a CDATA section node.

"akira" <akira.roshi@gmx.net> wrote in message
news:3e0bf833$1@tnews.web.dev-archive.com...
>
> "Lou" <l_kvitek@audiblemagic.com> wrote:
> >
> >Here's an XSLT that will do it:
> >
> ><xsl:stylesheet version="1.0"
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
> > xmlns:rs="urn:schemas-microsoft-com:rowset"
> > xmlns:z="#RowsetSchema">
> > <xsl:output indent="yes" method="xml"/>
> >
> > <xsl:template match="/">
> > <Rows>
> > <xsl:apply-templates select="xml/rs:data/z:row"/>
> > </Rows>
> > </xsl:template>
> >
> > <xsl:template match="xml/rs:data/z:row">
> > <Row>
> > <xsl:for-each select="@*">
> > <xsl:element name="{name()}">
> > <xsl:value-of select="."/>
> > </xsl:element>
> > </xsl:for-each>
> > </Row>
> > </xsl:template>
> >
> ></xsl:stylesheet>
>
> so why adding memo field would produce "An invalid character was found in
> text content."
>
> Is there any tips to return memo field with 'rich' character to an xml
> file ??
>
>
Russell Jones at 2007-11-11 23:32:48 >
# 5 Re: XML from ADO conversion to new XML via XSL
"Russell Jones" <arj1@nospam.northstate.net> wrote:
>The memo field probably contains XML reserved characters such as ampersands.
>You need to escape those characters before adding them to the XML document.

So I have two options here to escape those characters ...

1. Update each memo record to change the XML reserved characters to its entity
reff.

2. Make the XSL to do the "parsing" for me.

Is options no. 2 possible ? because I think that's the easier ...

>You may want to put such "free" text into a CDATA section node.

do you have any example for above 'tricks' ??

TIA

>"akira" <akira.roshi@gmx.net> wrote in message
>news:3e0bf833$1@tnews.web.dev-archive.com...
>>
>> "Lou" <l_kvitek@audiblemagic.com> wrote:
>> >
>> >Here's an XSLT that will do it:
>> >
>> ><xsl:stylesheet version="1.0"
>> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> > xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
>> > xmlns:rs="urn:schemas-microsoft-com:rowset"
>> > xmlns:z="#RowsetSchema">
>> > <xsl:output indent="yes" method="xml"/>
>> >
>> > <xsl:template match="/">
>> > <Rows>
>> > <xsl:apply-templates select="xml/rs:data/z:row"/>
>> > </Rows>
>> > </xsl:template>
>> >
>> > <xsl:template match="xml/rs:data/z:row">
>> > <Row>
>> > <xsl:for-each select="@*">
>> > <xsl:element name="{name()}">
>> > <xsl:value-of select="."/>
>> > </xsl:element>
>> > </xsl:for-each>
>> > </Row>
>> > </xsl:template>
>> >
>> ></xsl:stylesheet>
>>
>> so why adding memo field would produce "An invalid character was found
in
>> text content."
>>
>> Is there any tips to return memo field with 'rich' character to an xml
>> file ??
akira akira.roshiatgmx.net at 2007-11-11 23:33:53 >