.NET DataSet -> Xml + XSLT -> HTML
-
Hi all, I'm trying to convert a .NET DataSet into something viewable by a webbrowser. Its trivial to get the dataSet into XML format (.WriteXML), and then I need to transform that with an XSLT transformation. The problem is, the dataset comes back as xml like this:
... ADD_ADZIP164116PROCEDURE2003-06-19T14:08:03.0000000-04:002003-06-19T14:10:19.0000000-04:001998-12-22:11:10:19VALID ADD_LABEL164202FUNCTION2003-06-19T14:08:11.0000000-04:002003-06-19T14:10:20.0000000-04:001998-12-22:11:10:20INVALID ...
Which is fine but I get lost when it comes to transforming this data with an XSLT file. The problem is I need to know the element names (OBJECT_NAME, OBJECT_ID, etc) for the html file, but I do not want to hard-code these names into my .xslt file, nor do I wish to generate one on the fly. To give you some more background: All this app does is select some rows from a user defined table and outputs them to some files (CVS, tab delimited, and xml/html). The html is nothing fancy, I just want to dump them into a standard html table that is viewable in a browser, with field names at the top and data beneath it. This doesnt seem like it should be too hard to do. Every example of XSLT Ive seen is very clear and straightforward, I just havent been able to find anything about grabbing an elements name, and Im not sure how to change the format of the XML given by the dataset (which would be another solution - if I just formatted the xml differently). Any suggestions? Thanks Dave Ratti -
Hi all, I'm trying to convert a .NET DataSet into something viewable by a webbrowser. Its trivial to get the dataSet into XML format (.WriteXML), and then I need to transform that with an XSLT transformation. The problem is, the dataset comes back as xml like this:
... ADD_ADZIP164116PROCEDURE2003-06-19T14:08:03.0000000-04:002003-06-19T14:10:19.0000000-04:001998-12-22:11:10:19VALID ADD_LABEL164202FUNCTION2003-06-19T14:08:11.0000000-04:002003-06-19T14:10:20.0000000-04:001998-12-22:11:10:20INVALID ...
Which is fine but I get lost when it comes to transforming this data with an XSLT file. The problem is I need to know the element names (OBJECT_NAME, OBJECT_ID, etc) for the html file, but I do not want to hard-code these names into my .xslt file, nor do I wish to generate one on the fly. To give you some more background: All this app does is select some rows from a user defined table and outputs them to some files (CVS, tab delimited, and xml/html). The html is nothing fancy, I just want to dump them into a standard html table that is viewable in a browser, with field names at the top and data beneath it. This doesnt seem like it should be too hard to do. Every example of XSLT Ive seen is very clear and straightforward, I just havent been able to find anything about grabbing an elements name, and Im not sure how to change the format of the XML given by the dataset (which would be another solution - if I just formatted the xml differently). Any suggestions? Thanks Dave Rattiuse need to use the
name()
function in xslt. In order to not hard code element names you should pass them as parameters to your xslt. so something like:<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:param name="column_name">object_NAME</xsl:param> <xsl:template match="/"> <xsl:value-of select="//table/*[name() = $column_name]/text()"&/> </xsl:template> </xsl:stylesheet>
if you don't pass a parameter to your xslt, then it will default to 'object_NAME'. c# calling code:XslTransform xslt = new XslTransform(); XmlDocument document = new XmlDocument(); document.LoadXml(xmlFileName); xslt.Load(stylesheetFileName); MemoryStream ms = new MemoryStream(); XPathNavigator nav = document.CreateNavigator(); XsltArgumentList xslArg = new XsltArgumentList(); xslArg.AddParam("column_name", "", "**name of column you want**"); xslt.Transform(nav, xslArg, ms, null); // output to a stream StreamReader sr = new StreamReader(ms); ms.Position = 0; string result = sr.ReadToEnd(); sr.Close();
"When the only tool you have is a hammer, a sore thumb you will have."
-
use need to use the
name()
function in xslt. In order to not hard code element names you should pass them as parameters to your xslt. so something like:<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:param name="column_name">object_NAME</xsl:param> <xsl:template match="/"> <xsl:value-of select="//table/*[name() = $column_name]/text()"&/> </xsl:template> </xsl:stylesheet>
if you don't pass a parameter to your xslt, then it will default to 'object_NAME'. c# calling code:XslTransform xslt = new XslTransform(); XmlDocument document = new XmlDocument(); document.LoadXml(xmlFileName); xslt.Load(stylesheetFileName); MemoryStream ms = new MemoryStream(); XPathNavigator nav = document.CreateNavigator(); XsltArgumentList xslArg = new XsltArgumentList(); xslArg.AddParam("column_name", "", "**name of column you want**"); xslt.Transform(nav, xslArg, ms, null); // output to a stream StreamReader sr = new StreamReader(ms); ms.Position = 0; string result = sr.ReadToEnd(); sr.Close();
"When the only tool you have is a hammer, a sore thumb you will have."
-
Great, thanks. One more question - what if I dont know how many columns the table is going to have? I think I might be able to hack something together with mxlns:script, unless anyone has a better idea? Thanks Dave Ratti
as you guessed you will have to scripting to do this - version 1 of xsl is pretty bare bones...
"When the only tool you have is a hammer, a sore thumb you will have."
-
as you guessed you will have to scripting to do this - version 1 of xsl is pretty bare bones...
"When the only tool you have is a hammer, a sore thumb you will have."