Connecting Tech Pros Worldwide Forums | Help | Site Map

Xslt transform in code yields nothing

Andy
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi all,

I'm having a problem doing an Xslt transform in code. I've done it
before, so I'm not really sure why its not working. The problem is
that the result of the transform is an empty string. I expected the
xml to be transformed into a plain text document. Everything works
fine when i transform using XmlPad. Here's the code (which does
generate the Xml properly):

Sample XML:
<?xml version="1.0" encoding="utf-8" ?>
<dalGen>
<table name="test" className="Test" provider="SqlServer">
<columns>
<column name="Id" propertyName="Id" dataType="int" isKeyPart="true"
isReadOnly="false" />
<column name="FirstName" propertyName="FirstName" dataType="string"
isKeyPart="false" isReadOnly="false" />
<column name="LastName" propertyName="LastName" dataType="string"
isKeyPart="false" isReadOnly="false" />
<column name="Ssn" propertyName="Ssn" dataType="string"
isKeyPart="false" isReadOnly="false" />
</columns>
</table>
</dalGen>

The XSLT:
<?xml version="1.0" encoding="UTF-8" ?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" media-type="text/plain"
omit-xml-declaration="yes" />

<xsl:template match="dalGen">
using System;

using Ccp.Core.Data;
using Ccp.Core.Data.Providers;

namespace Ccp.Core.Data.Entities {
<xsl:apply-templates />
}
</xsl:template>

<xsl:template match="table">

public class <xsl:value-of select="./@className" /> : <xsl:value-of
select="./@provider" />Entity {
<xsl:apply-templates mode="fields" select="columns" />
<xsl:apply-templates mode="properties" select="columns" />
}

</xsl:template>

<xsl:template match="columns" name="DataFields" mode="fields">
#region DataFields
<xsl:apply-templates select="column" mode="fields" />
#endregion
</xsl:template>

<xsl:template match="column" name="DataField" mode="fields">
/// <summary>Stores the value of the field <c><xsl:value-of
select="./@name" /></c>.</summary>
protected <xsl:value-of select="./@dataType" /> _<xsl:value-of
select="./@propertyName" />;
</xsl:template>

<xsl:template match="columns" name="Properties" mode="properties">
#region Properties
<xsl:apply-templates select="column" mode="properties" />
#endregion
</xsl:template>

<xsl:template match="column" name="Property" mode="properties">
/// <summary>Gets or sets the <c><xsl:value-of select="./@name"
/></c>.</summary>
[Persisted( <xsl:value-of select="./@isKeyPart" />, "<xsl:value-of
select="./@name" />", <xsl:value-of select="./@isReadOnly" /> )]
public <xsl:value-of select="./@dataType" /><xsl:text>
</xsl:text><xsl:value-of select="./@name" /> {
get { return _<xsl:value-of select="./@propertyName" />; }
set { _<xsl:value-of select="./@propertyName" /> = value; }
}
</xsl:template>
</xsl:stylesheet>


The C# code:
public override string GenerateDALClass( string tableName ) {
XslTransform xsl;
XmlTextReader reader;
MemoryStream output;
StreamReader outReader;
XmlDocument xml;
string result;

reader = new XmlTextReader(
Assembly.GetExecutingAssembly().GetManifestResourc eStream(
GetType(),
"DalClass.xslt"
)
);

xsl = new XslTransform();
xsl.Load( reader, null, GetType().Assembly.Evidence );

xml = BuildTableXml( tableName );

using( output = new MemoryStream() ) {
xsl.Transform(
xml.CreateNavigator(),
null,
output,
null
);

using( outReader = new StreamReader( output ) ) {
result = outReader.ReadToEnd();
}
}

return result;
}



Thanks for any help.
Andy


Pranav Kandula
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Xslt transform in code yields nothing


Since I don't have access to your BuildTableXml() method and other custom
stuff, I simply took your XML and XSLT into 2 separate files applied
XslTransform over it and it does produce the expected output (C# code)

Can you debug your application and see if you are BuildTableXml is returning
good Xml ?

Thanks
Pranav

"Andy" <ajohnstone@capcitypress.com> wrote in message
news:1129664333.559528.100070@g43g2000cwa.googlegr oups.com...[color=blue]
> Hi all,
>
> I'm having a problem doing an Xslt transform in code. I've done it
> before, so I'm not really sure why its not working. The problem is
> that the result of the transform is an empty string. I expected the
> xml to be transformed into a plain text document. Everything works
> fine when i transform using XmlPad. Here's the code (which does
> generate the Xml properly):
>
> Sample XML:
> <?xml version="1.0" encoding="utf-8" ?>
> <dalGen>
> <table name="test" className="Test" provider="SqlServer">
> <columns>
> <column name="Id" propertyName="Id" dataType="int" isKeyPart="true"
> isReadOnly="false" />
> <column name="FirstName" propertyName="FirstName" dataType="string"
> isKeyPart="false" isReadOnly="false" />
> <column name="LastName" propertyName="LastName" dataType="string"
> isKeyPart="false" isReadOnly="false" />
> <column name="Ssn" propertyName="Ssn" dataType="string"
> isKeyPart="false" isReadOnly="false" />
> </columns>
> </table>
> </dalGen>
>
> The XSLT:
> <?xml version="1.0" encoding="UTF-8" ?>
>
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:output method="text" media-type="text/plain"
> omit-xml-declaration="yes" />
>
> <xsl:template match="dalGen">
> using System;
>
> using Ccp.Core.Data;
> using Ccp.Core.Data.Providers;
>
> namespace Ccp.Core.Data.Entities {
> <xsl:apply-templates />
> }
> </xsl:template>
>
> <xsl:template match="table">
>
> public class <xsl:value-of select="./@className" /> : <xsl:value-of
> select="./@provider" />Entity {
> <xsl:apply-templates mode="fields" select="columns" />
> <xsl:apply-templates mode="properties" select="columns" />
> }
>
> </xsl:template>
>
> <xsl:template match="columns" name="DataFields" mode="fields">
> #region DataFields
> <xsl:apply-templates select="column" mode="fields" />
> #endregion
> </xsl:template>
>
> <xsl:template match="column" name="DataField" mode="fields">
> /// <summary>Stores the value of the field <c><xsl:value-of
> select="./@name" /></c>.</summary>
> protected <xsl:value-of select="./@dataType" /> _<xsl:value-of
> select="./@propertyName" />;
> </xsl:template>
>
> <xsl:template match="columns" name="Properties" mode="properties">
> #region Properties
> <xsl:apply-templates select="column" mode="properties" />
> #endregion
> </xsl:template>
>
> <xsl:template match="column" name="Property" mode="properties">
> /// <summary>Gets or sets the <c><xsl:value-of select="./@name"
> /></c>.</summary>
> [Persisted( <xsl:value-of select="./@isKeyPart" />, "<xsl:value-of
> select="./@name" />", <xsl:value-of select="./@isReadOnly" /> )]
> public <xsl:value-of select="./@dataType" /><xsl:text>
> </xsl:text><xsl:value-of select="./@name" /> {
> get { return _<xsl:value-of select="./@propertyName" />; }
> set { _<xsl:value-of select="./@propertyName" /> = value; }
> }
> </xsl:template>
> </xsl:stylesheet>
>
>
> The C# code:
> public override string GenerateDALClass( string tableName ) {
> XslTransform xsl;
> XmlTextReader reader;
> MemoryStream output;
> StreamReader outReader;
> XmlDocument xml;
> string result;
>
> reader = new XmlTextReader(
> Assembly.GetExecutingAssembly().GetManifestResourc eStream(
> GetType(),
> "DalClass.xslt"
> )
> );
>
> xsl = new XslTransform();
> xsl.Load( reader, null, GetType().Assembly.Evidence );
>
> xml = BuildTableXml( tableName );
>
> using( output = new MemoryStream() ) {
> xsl.Transform(
> xml.CreateNavigator(),
> null,
> output,
> null
> );
>
> using( outReader = new StreamReader( output ) ) {
> result = outReader.ReadToEnd();
> }
> }
>
> return result;
> }
>
>
>
> Thanks for any help.
> Andy
>[/color]


Andy
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Xslt transform in code yields nothing


Sure,

This Xml is generated from the BuildTableXml:

<?xml version="1.0" encoding="utf-8"?>
<dalGen>
<table name="Employees" className="Employees" provider="SqlServer">
<columns>
<column name="EmployeeID" propertyName="Employeeid"
dataType="int" isKeyPart="true" isReadOnly="true"/>
<column name="LastName" propertyName="Lastname" dataType="string"
isKeyPart="false" isReadOnly="false"/>
<column name="FirstName" propertyName="Firstname"
dataType="string" isKeyPart="false" isReadOnly="false"/>
<column name="Title" propertyName="Title" dataType="string"
isKeyPart="false" isReadOnly="false"/>
<column name="TitleOfCourtesy" propertyName="Titleofcourtesy"
dataType="string" isKeyPart="false" isReadOnly="false"/>
<column name="BirthDate" propertyName="Birthdate"
dataType="System.DateTime" isKeyPart="false" isReadOnly="false"/>
<column name="HireDate" propertyName="Hiredate"
dataType="System.DateTime" isKeyPart="false" isReadOnly="false"/>
<column name="Address" propertyName="Address" dataType="string"
isKeyPart="false" isReadOnly="false" />
<column name="City" propertyName="City" dataType="string"
isKeyPart="false" isReadOnly="false"/>
<column name="Region" propertyName="Region" dataType="string"
isKeyPart="false" isReadOnly="false"/>
<column name="PostalCode" propertyName="Postalcode"
dataType="string" isKeyPart="false" isReadOnly="false"/>
<column name="Country" propertyName="Country" dataType="string"
isKeyPart="false" isReadOnly="false"/>
<column name="HomePhone" propertyName="Homephone"
dataType="string" isKeyPart="false" isReadOnly="false"/>
<column name="Extension" propertyName="Extension"
dataType="string" isKeyPart="false" isReadOnly="false"/>
<column name="Photo" propertyName="Photo"
dataType="System.Byte[]" isKeyPart="false" isReadOnly="false"/>
<column name="Notes" propertyName="Notes" dataType="string"
isKeyPart="false" isReadOnly="false"/>
<column name="ReportsTo" propertyName="Reportsto" dataType="int"
isKeyPart="false" isReadOnly="false"/>
<column name="PhotoPath" propertyName="Photopath"
dataType="string" isKeyPart="false" isReadOnly="false"/>
<column name="msrepl_tran_version"
propertyName="Msrepl_Tran_Version" dataType="object" isKeyPart="false"
isReadOnly="false"/>
</columns>
</table>
</dalGen>


Thanks
Andy

Andy
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Xslt transform in code yields nothing


I found the solution, it was a silly mistake.

The MemoryStream output was just written into, so you need to seek back
to the beginning of the stream before you try to read it. Once i added
that, things work fine.

Thanks
Andy

Closed Thread