XSLT
--------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html >
<head>
<title>Untitled Page</title>
<style type="text/css">
.style2
{
width: 320px;
font-weight: bold;
}
.style3
{
width: 9%;
}
.tableStyle
{
width:100%; font-family: sans-serif;
font-size: 8.25px;
font-weight: normal;
}
</style>
</head>
<body bgcolor="#e7ecff" style =" font: 10pt sans-serif; font-style:
normal;" >
<br/>
<table width="100%">
<!--SOCIAL HISTORY-->
<xsl:for-each select="Assessment/PersonalSocialHistory">
XML
--------
<Assessment
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Cardiovascular><AssessmentDateTime>2009-02-25T00:13:23.437</AssessmentDateTime><AssessmentIdentifier>1</AssessmentIdentifier><BlnEdemaLocationIsEnabled>false</BlnEdemaLocationIsEnabled><BlnEdemaOtherIsEnabled>false</BlnEdemaOtherIsEnabled><BlnPacemakerIsEnabled>false</BlnPacemakerIsEnabled><BlnPulsesEnabled>true</BlnPulsesEnabled><BlnReproducibleIsEnabled>false</BlnReproducibleIsEnabled><BruitsLocation
i:nil="true"/><CADelayedTime i:nil="true"/><CRNormalDelayed
i:nil="true"/><CapillaryRefill>1
</CapillaryRefill><CapillaryRefillGreaterThan3W
i:nil="true"/><CapillaryRefillLessThan3W
i:nil="true"/><CardioID>244</CardioID><CardioNotes
i:nil="true"/><CardioStatus>A</CardioStatus><ChestPain>1 </ChestPain>
.
.
.
Thanks in Advance..
> I am new to XSL.Could some one please tell me whay i am seeing ??? in my
> output HTML?
How are you looking at the resulting HTML? Which XSLT processor do you use?
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
> Like i said i am new to xslt..Not sure what a processor is...I am
> displaying the output html in a Microsoft Webbrowser control in a Windows
> form(C#).I created the XSLT using the Vstudio -> Add new item -> xslt
> got the xml by using DataContractSerializer class since the entity is a
> DataContract object.
How do you create the output HTML exactly? How do you load it in the
WebBrowser control?
public class XmlGenerator<T> where T : class
{
private T _dataContract;
public XmlGenerator(T dataContract)
{
_dataContract = dataContract;
}
/// <summary>
/// Generates the XML from the Object
/// </summary>
/// <returns></returns>
private string GenerateXml()
{
UnicodeEncoding uniEncoding = new UnicodeEncoding();
string buffer;
DataContractSerializer ser = new
DataContractSerializer(typeof(T));
StreamWriter stWriter = null;
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream,
Encoding.Unicode);
stWriter = new StreamWriter(memoryStream);
ser.WriteObject(memoryStream, this._dataContract);
buffer = Encoding.ASCII.GetString(memoryStream.GetBuffer());
return
buffer.Replace(@"xmlns=""http://schemas.datacontract.org/2004/07/EDIM.DataContracts""", "");
}
public string TransformXmlFinal(string xSLTFileName)
{
XmlDataDocument d = new XmlDataDocument();
string sXML = this.GenerateXml();
d.LoadXml(this.GenerateXml());
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(xSLTFileName);
MemoryStream memoryStreamOutPut = new MemoryStream();
xslTransform.Transform(d, null, memoryStreamOutPut);
return Encoding.ASCII.GetString(memoryStreamOutPut.GetBuffer());
}
}
Thanks for your help
..
Don't use all those MemoryStreams, if you want a string then you can use
a StringWriter, there is no need to mess with MemoryStreams, Encodings,
byte arrays:
private string GenerateXml()
{
string generatedXml;
DataContractSerializer ser = new
DataContractSerializer(typeof(T));
using (StringWriter sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(sw))
{
ser.WriteObject(xw, this._dataContract);
xw.Close();
}
generatedXml = sw.ToString();
}
return generatedXml;
}
public string TransformXmlFinal(string xSLTFileName)
{
string sXML = this.GenerateXml();
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(xSLTFileName);
StringWriter result = new StringWriter();
xslTransform.Transform(new XPathDocument(new
StringReader(sXML)), null, result);
return result.ToString();
}
That way I hope the question marks should no longer appear as I suspect
they occured by using Encoding.ASCII to decode a byte array in a
different encoding and perhaps containing a byte order mark.
since without that, for some reason gave me a blank html output.
Well that "hack" is certainly not necessary if you learn to write XSLT
1.0 stylesheet dealing with default namespace declarations. So instead
of removing the xmlns declaration with string processing you would need
to fix your XSLT stylesheet to bind a prefix to that namespace and use
that prefix in XPath expressions and in XSLT match patterns to qualify
element names e.g.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:dc="http://schemas.datacontract.org/2004/07/EDIM.DataContracts"
exclude-result-prefixes="dc">
<!-- now use prefix 'dc' in expressions and patterns -->
<xsl:template match="dc:foo">
<xsl:apply-templates select="dc:bar/">
<xsl:template>
<xsl:template match="dc:bar">
<xsl:value-of select="dc:baz"/>
</xsl:template>
</xsl:template>
So basically, in your stylesheet you need to replace all unqualified
element names 'foo' by 'dc:foo'. That way your stylesheet would work
without any need to preprocess the XML input with string replacement.
If you don't understand what I am suggesting above then post your XML
input and your stylesheet, then we can show you how to fix that.