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 3 2108
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" <aj********@capcitypress.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Johannes Lebek |
last post by:
Hi there,
somehow, I cannot access nodes that are stored in a variable. I'm using
Xalan 2.5.1 and the following commands:
================ BEGIN ====================
<xsl:variable...
|
by: Harry Zoroc |
last post by:
I would like to treat an xsd Schema file as XML file and to display the targetNamespace and all the imports. That's it.
But the following does not work. Why?
I did not enter the stylesheet in the...
|
by: Wil |
last post by:
I'm very new to developing in .NET and even newer to XML. The past few days
have been pretty frustrating for me because I'm trying to perform a transform
on data in a dataset and it's not working....
|
by: Moogy |
last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't
help, but none of this makes any sense to me. All I'm trying to do is
take a simple source XML file and translate it with an...
|
by: Christopher M. Lauer |
last post by:
I have done my best to answer this question but can not find the
proper set of commands. I would like
to transform an xml file (in code behind) and display its output in a
specific html tag,...
|
by: Doug Heeren |
last post by:
I have the following section of VB.NET code that transforms a simple dataset
into an Excel xml workbook. It works fine for < 50 rows or so, but I have
about 8,000 rows I need to transform. Is there...
|
by: One Handed Man \( OHM - Terry Burns \) |
last post by:
I've been battling with this stupid problem for hours now.
WebApp:
Trying to do a simple transformation using XSLT to a Web Page, but it just
failes without an error message ( In other words,...
|
by: Terry Brown |
last post by:
I have an xml file:
<?xml version="1.0" encoding="utf-8" ?>
<G2Registers
xmlns="http://tempuri.org/registers.xsd">
<register>
<name>Version Register</name>
<address>"00000000"</address>...
|
by: darrel |
last post by:
I don't do a lot with XML, so I always get a bit lost navigating the XML
classes.
In the past, I've used this:
xslt.Transform(doc, xslArg, Response.Output, Nothing)
Which takes my xml file...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |