Thursday, 30 May 2013

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>



Friday, 16 November 2012

Generates a square root of a value using babylonian method


<?xml version="1.0" encoding="UTF-8"?>
<!--
    generates a square root of a value using babylonian method.
-->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:output method="xml"/>
    <xsl:template match="root">
        <out>
            <xsl:for-each select="value">
                <xsl:call-template name="approx-root">
                    <xsl:with-param name="value" select="."/>
                    <xsl:with-param name="guess" select="number(.) div 2"/>
                </xsl:call-template>
            </xsl:for-each>
        </out>
    </xsl:template>
 
    <xsl:template name="approx-root">
        <xsl:param name="value"/>
        <xsl:param name="guess"/>
        <xsl:param name="level">10</xsl:param>
     
        <xsl:if test="$level = 0">
            <xsl:value-of select="$guess"/>
        </xsl:if>
     
        <xsl:if test="$level &gt; 0">
            <xsl:call-template name="approx-root">
                <xsl:with-param name="guess" select="($guess + $value div $guess) div 2"/>
                <xsl:with-param name="value" select="$value"/>
                <xsl:with-param name="level" select="$level - 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
 
</xsl:stylesheet>