EDN Admin
Well-known member
Here is the method that tranform xml to xsl.Please let me know where its failing?
public void ConverttoCSV_Click(object sender, EventArgs e)<br/>
{<br/>
string xsl = @"C:test.xsl";<br/>
XmlReader rds = XmlReader.Create(@"C:test.xml");<br/>
XPathDocument doc = new XPathDocument(rds);<br/>
<br/>
XslCompiledTransform xslt = new XslCompiledTransform();<br/>
xslt.Load(xsl);<br/>
XmlWriter wrt = XmlWriter.Create(@"C:test.csv");<br/>
xslt.Transform(rds, wrt);<br/>
MessageBox.Show("Success");<br/>
rds.Close();<br/>
wrt.Close();<br/>
}
Here is the .XSL template File:
<?xml version="1.0"?><br/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv <br/>
<xslutput method="text" encoding="utf-8" /><br/>
<xsl:strip-space elements="*" /><br/>
<br/>
<xsl:variable name="delimiter" select="," /><br/>
<br/>
<csv:columns><br/>
<column>name</column><br/>
<column>sublease</column><br/>
<column>addressBookID</column><br/>
<column>boundAmount</column><br/>
<column>rentalAmount</column><br/>
<column>rentalPeriod</column><br/>
<column>rentalBillingCycle</column><br/>
<column>tenureIncome</column><br/>
<column>tenureBalance</column><br/>
<column>totalIncome</column><br/>
<column>balance</column><br/>
<column>available</column><br/>
</csv:columns><br/>
<br/>
<xsl:template match="/property-manager/properties <br/>
<!-- Output the CSV header --><br/>
<xsl:for-each select="document()/*/csv:columns/* <br/>
<xsl:value-of select="."/><br/>
<xsl:if test="position() != last() <br/>
<xsl:value-of select="$delimiter"/><br/>
</xsl:if><br/>
</xsl:for-each><br/>
<xsl:text>
</xsl:text><br/>
<br/>
<!-- Output rows for each matched property --><br/>
<xsl:apply-templates select="property" /><br/>
</xsl:template><br/>
<br/>
<xsl:template match="property <br/>
<xsl:variable name="property" select="." /><br/>
<br/>
<!-- Loop through the columns in order --><br/>
<xsl:for-each select="document()/*/csv:columns/* <br/>
<!-- Extract the column name and value --><br/>
<xsl:variable name="column" select="." /><br/>
<xsl:variable name="value" select="$property/*[name() = $column]" /><br/>
<br/>
<!-- Quote the value if required --><br/>
<xsl:choose><br/>
<xsl:when test="contains($value, " <br/>
<xsl:variable name="x" select="translate($value, ", """/><br/>
<xsl:value-of select="concat(", $x, ""/><br/>
</xsl:when><br/>
<xsl:when test="contains($value, $delimiter) <br/>
<xsl:value-of select="concat(", $value, ""/><br/>
</xsl:when><br/>
<xsltherwise><br/>
<xsl:value-of select="$value"/><br/>
</xsltherwise><br/>
</xsl:choose><br/>
<br/>
<!-- Add the delimiter unless we are the last expression --><br/>
<xsl:if test="position() != last() <br/>
<xsl:value-of select="$delimiter"/><br/>
</xsl:if><br/>
</xsl:for-each><br/>
<br/>
<!-- Add a newline at the end of the record --><br/>
<xsl:text>
</xsl:text><br/>
</xsl:template><br/>
<br/>
</xsl:stylesheet>
<span style="white-spacere t<span style="white-spacere hanks in advance
<hr class="sig satwick
View the full article
public void ConverttoCSV_Click(object sender, EventArgs e)<br/>
{<br/>
string xsl = @"C:test.xsl";<br/>
XmlReader rds = XmlReader.Create(@"C:test.xml");<br/>
XPathDocument doc = new XPathDocument(rds);<br/>
<br/>
XslCompiledTransform xslt = new XslCompiledTransform();<br/>
xslt.Load(xsl);<br/>
XmlWriter wrt = XmlWriter.Create(@"C:test.csv");<br/>
xslt.Transform(rds, wrt);<br/>
MessageBox.Show("Success");<br/>
rds.Close();<br/>
wrt.Close();<br/>
}
Here is the .XSL template File:
<?xml version="1.0"?><br/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv <br/>
<xslutput method="text" encoding="utf-8" /><br/>
<xsl:strip-space elements="*" /><br/>
<br/>
<xsl:variable name="delimiter" select="," /><br/>
<br/>
<csv:columns><br/>
<column>name</column><br/>
<column>sublease</column><br/>
<column>addressBookID</column><br/>
<column>boundAmount</column><br/>
<column>rentalAmount</column><br/>
<column>rentalPeriod</column><br/>
<column>rentalBillingCycle</column><br/>
<column>tenureIncome</column><br/>
<column>tenureBalance</column><br/>
<column>totalIncome</column><br/>
<column>balance</column><br/>
<column>available</column><br/>
</csv:columns><br/>
<br/>
<xsl:template match="/property-manager/properties <br/>
<!-- Output the CSV header --><br/>
<xsl:for-each select="document()/*/csv:columns/* <br/>
<xsl:value-of select="."/><br/>
<xsl:if test="position() != last() <br/>
<xsl:value-of select="$delimiter"/><br/>
</xsl:if><br/>
</xsl:for-each><br/>
<xsl:text>
</xsl:text><br/>
<br/>
<!-- Output rows for each matched property --><br/>
<xsl:apply-templates select="property" /><br/>
</xsl:template><br/>
<br/>
<xsl:template match="property <br/>
<xsl:variable name="property" select="." /><br/>
<br/>
<!-- Loop through the columns in order --><br/>
<xsl:for-each select="document()/*/csv:columns/* <br/>
<!-- Extract the column name and value --><br/>
<xsl:variable name="column" select="." /><br/>
<xsl:variable name="value" select="$property/*[name() = $column]" /><br/>
<br/>
<!-- Quote the value if required --><br/>
<xsl:choose><br/>
<xsl:when test="contains($value, " <br/>
<xsl:variable name="x" select="translate($value, ", """/><br/>
<xsl:value-of select="concat(", $x, ""/><br/>
</xsl:when><br/>
<xsl:when test="contains($value, $delimiter) <br/>
<xsl:value-of select="concat(", $value, ""/><br/>
</xsl:when><br/>
<xsltherwise><br/>
<xsl:value-of select="$value"/><br/>
</xsltherwise><br/>
</xsl:choose><br/>
<br/>
<!-- Add the delimiter unless we are the last expression --><br/>
<xsl:if test="position() != last() <br/>
<xsl:value-of select="$delimiter"/><br/>
</xsl:if><br/>
</xsl:for-each><br/>
<br/>
<!-- Add a newline at the end of the record --><br/>
<xsl:text>
</xsl:text><br/>
</xsl:template><br/>
<br/>
</xsl:stylesheet>
<span style="white-spacere t<span style="white-spacere hanks in advance
<hr class="sig satwick
View the full article