Monday 5 August 2013

XSLT 2 Find Occurence of each distinct character in a string

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
   
    <!--Two ways two find the occurrence of distnict character in a string-->
   
    <xsl:template match="/">
        <xsl:variable name="value" select="'Vishnu singh shekhawat'"/>
        <xsl:variable name="EachChar">
        <xsl:analyze-string select="." regex="\c">
            <xsl:matching-substring>
                <a><xsl:value-of select="."/></a>
            </xsl:matching-substring>
        </xsl:analyze-string>
        </xsl:variable>
        <xsl:for-each select="distinct-values($EachChar/a)">
            <xsl:sort select="." case-order="lower-first" data-type="text"/>
            <xsl:value-of select="."/> is <xsl:value-of select="count(tokenize($value,.)) - 1"/> <xsl:text> times &#x0A;</xsl:text>   
        </xsl:for-each>
    </xsl:template>
   
   
    <xsl:template match="/">
        <xsl:variable name="value" select="translate('Vishnu singh shekhawat',' ','')"/>
        <xsl:variable name="codePoints" select="string-to-codepoints($value)"/>
        <xsl:variable name="DistnictValue" select="distinct-values($codePoints)"/>
        <xsl:for-each select="$DistnictValue">
            <xsl:value-of select="codepoints-to-string(.)"/> is <xsl:value-of select="string-length($value) - string-length(translate($value,codepoints-to-string(.),''))"/><xsl:value-of select="'&#x0A;'"></xsl:value-of>
        </xsl:for-each>
    </xsl:template>
   
</xsl:stylesheet>

Thursday 30 May 2013

How to Generate Excel with XSLT

<xsl:stylesheet version="1.0"
    xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >  
   

    <xsl:template match="/">
        <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
            xmlns:o="urn:schemas-microsoft-com:office:office"
            xmlns:x="urn:schemas-microsoft-com:office:excel"
            xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
            xmlns:html="http://www.w3.org/TR/REC-html40">
           
            <Styles>
                <Style ss:ID="Default" ss:Name="Normal">
                    <Alignment ss:Vertical="Bottom"/>
                    <Borders/>
                    <Font/>
                    <Interior/>
                    <NumberFormat/>
                    <Protection/>
                </Style>
                <Style ss:ID="s21">
                    <Font ss:Bold="1"/>
                    <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
                </Style>
                <Style ss:ID="s22">
                    <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
                    <Font ss:Bold="1"/>
                    <Interior ss:Color="#99CCFF" ss:Pattern="Solid"/>
                </Style>
                <Style ss:ID="s23" ss:Name="Currency">
                    <NumberFormat
                        ss:Format="_(&quot;$&quot;* #,##0.00_);_(&quot;$&quot;* \(#,##0.00\);_(&quot;$&quot;* &quot;-&quot;??_);_(@_)"/>
                </Style>
                <Style ss:ID="s24">
                    <NumberFormat ss:Format="_(* #,##0.00_);_(* \(#,##0.00\);_(* &quot;-&quot;??_);_(@_)"/>
                </Style>
                <Style ss:ID="s25">
                    <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
                </Style>
            </Styles>
           
            <Worksheet>
                <xsl:attribute name="ss:Name">
                    <xsl:value-of select='concat("Order #", "Customer")'/>
                </xsl:attribute>
                <Table ss:ExpandedColumnCount="3">
                    <xsl:attribute name="ss:ExpandedRowCount" >
                        <xsl:value-of select="10"/>
                    </xsl:attribute>
                   
                    <Column ss:AutoFitWidth="0" ss:Width="150"/>
                    <Column ss:AutoFitWidth="0" ss:Width="100"/>
                    <Column ss:AutoFitWidth="0" ss:Width="75"/>
                   
                    <Row>
                        <Cell ss:StyleID="s21"><Data ss:Type="String">Item</Data></Cell>
                        <Cell ss:StyleID="s21"><Data ss:Type="String">Quantity</Data></Cell>
                        <Cell ss:StyleID="s21"><Data ss:Type="String">Total</Data></Cell>
                    </Row>
                   
                    <Row>
                        <Cell ss:Index="2"><Data ss:Type="String">Subtotal</Data></Cell>
                        <Cell ss:StyleID="s23" ss:Formula="=SUM(R8C:R[-1]C)"/>
                    </Row>
                    <Row>
                        <Cell ss:Index="2"><Data ss:Type="String">Freight</Data></Cell>
                        <Cell ss:StyleID="s23"><Data ss:Type="Number"><xsl:value-of select="20000"/></Data></Cell>
                    </Row>
                    <Row>
                        <Cell ss:Index="2"><Data ss:Type="String">Total</Data></Cell>
                        <Cell ss:StyleID="s23" ss:Formula="=R[-2]C+R[-1]C"/>
                    </Row>
                </Table>
            </Worksheet>
        </Workbook>
       
    </xsl:template>

</xsl:stylesheet>

Transforming XML into MS Word Document using XSLT

XML Input:-

<?xml version="1.0" encoding="UTF-8"?>
<dradis>
    <notes>
        <note>
            <fields>
                <Title>Shekhawat</Title>
</fields>
        </note>
        <note>
            <fields>
                <Title>Vishnu</Title>
            </fields>
        </note>
        <note>
            <fields>
                <Title>Singh</Title>
            </fields>
        </note>
    </notes>
</dradis>

XSLT Script:-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
       
        <xsl:processing-instruction name="mso-application">
            <xsl:text>progid="Word.Document"</xsl:text>
        </xsl:processing-instruction>
        <w:wordDocument
            xmlns:w="http://schemas.microsoft.com/office/word/2007/wordml">           
            <w:body>               
        <xsl:for-each select="dradis/notes/note/fields">
                    <w:p>
                        <w:r>
                            <w:t><xsl:value-of select="Title"/></w:t>
                        </w:r>
                        </w:p>
                </xsl:for-each>
            </w:body>
        </w:wordDocument>       
    </xsl:template>
</xsl:stylesheet>