Showing posts with label XSL. Show all posts
Showing posts with label XSL. Show all posts

Friday, July 2, 2010

How To Handdle XSL output HTML charset

Some times we transfrom xsl to html will get a wrong html chartset.
In C# we usually using TextWriter or XmlTextWriter to output a stream from xsl transformation, but the default encoding of the TextWriter and XmlTextWriter have a potential pitfall makes you difficult to fine out the real reason cause the wrong html charset automatically reander by xsl transform.
By defaul TextWriter and XmlTextWriter using UTF-16 encoding, it cause XSL Trasformation automatically generate html charset to UTF-16, What if you already defined html chartset in your xsl you will find out there a duplicated content-type defined in your output html head like this:
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-16" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        .....
        .....
Solution:
      
      no more best guidance than a real sample code....
XmlDocument xDoc = new XmlDocument();
xDoc.Load(new Uri(Request.Url, xmlUrl).ToString());
XslCompiledTransform xTrans = new XslCompiledTransform();
xTrans.Load(new Uri(Request.Url, xslUrl).ToString());
StringBuilder sb = new StringBuilder();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
{
    Encoding = Encoding.UTF8
    ,
    // if you don't want any xml declaration output such as <?xml ... ?>
    OmitXmlDeclaration = true    
};
          XmlWriter xWriter = XmlTextWriter.Create(sb, xmlWriterSettings);
          xTrans.Transform(xDoc, null, xWriter);
        
         // get result there
        System.Console.Write(sb.ToString());
       
        xWriter.Close();
        // if you want directly write to response
        XmlTextWriter xTxtWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        xTxtWriter.Close();

Saturday, May 15, 2010

XSLT Samples

<List>
    <Item name="xslt sample1">
        <fieldName>sTitle</fieldName>
        <Value>This is Sample fo xslt</Value>
    </Item>
    <Item name="xslt sample2">
        <fieldName>PostDate</fieldName>
        <Value>This is Sample fo xslt</Value>
    </Item>
</List>

<xsl:value-of select="."/>
<xsl:value-of select="List/Item/@name"/>
<xsl:value-of select=""List/Item[@name='xslt sample2']" />
<xsl:value-of select=""List/Item[fieldName='PostDate']/Value" />
<xsl:value-of select="parent::Item/@srId" />
<xsl:if test="string(number(rssCount))!='NaN'"> .... </xsl:if>

................................................................................................
<xsl:template match="Item/Value">
    <xsl:value-of select="text()"/>
</xsl:template>

<!-- HTML textarea closed tag issue-->
<textarea><xsl:text>&#xA;</xsl:text></textarea>

<!--Get TagName -->
<xsl:value-of select="name()"/>

<xsl:if test="count(Item) < 3"> The Item has less than 3 elements. </xsl:if>


<xsl:if test="count(Item) &gt; 3"> The Item has more than 3 elements. </xsl:if>

About Using <!CDATA[[ ... ]]> If you want to output a text wrapped by <!CDATA[[]]> to another xml, you can do this as below.
Some times you want to output it to an HTML through xsl transformer, the transformer will not to proccess the CDATA been output in a text as below. 
<script type="text/javascript"> 
 
    // the transformer result should look like:
    // <!--
    //     Some scripting code here... 
    // -->
    <xsl:text disable-output-escaping="yes">&lt;!--&gt;&lt;![CDATA[//&gt;&lt;!--</xsl:text> 
        Some scripting code here...
    <xsl:text disable-output-escaping="yes">//--&gt;]]&gt;</xsl:text>
 
    // this trans result same as above but more simple and clear 
    <xsl:comment><![CDATA[        Some scripting code here...    ]]> </xsl:comment> 
</script>
 
If you want the xsl output wrapped by <!CDATA[[ ... ]]>, Only one way i knew can do it that is cdata-section-elements.
You can add cdata-section-elements in xsl:output, each cdata-section-elements could contains one valu, but you can 
add more than one xsl:output, see the sample.
 
<xsl:output method="html" cdata-section-elements="title" />
<xsl:output cdata-section-elements="content" />
.....
<xsl:template match="content">
   your content here....
</xsl:template>
....
<xsl:apply-templates select="content" />
 
xsl output is:
<!CDATA[[your content here....]]>
 
Related
http://www.w3schools.com/xsl/default.asp
http://msdn.microsoft.com/en-us/library/ms256086%28VS.85%29.aspx
http://www.dpawson.co.uk/xsl/sect2/N8413.html
http://www.xml.com/pub/a/2003/04/02/trxml.html
http://www.brics.dk/~amoeller/XML/transformation/examples.html