#1999 - XSLT
1999 - XSLT
The World Wide Web Consortium (W3C) created XSLT for transforming XML into HTML, text, etc. The following examples assume input is enclosed in <input>..</input> tags.
##Task 1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/input">XSLT was made in 1999!</xsl:template>
</xsl:stylesheet>
This
Task 1
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/input">XSLT was made in 1999!</xsl:template>
</xsl:stylesheet>
This one is simple. It matches an input tag at the top level and replaces it with the desired output.
##Task 2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/input">
<xsl:call-template name="loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="n">
<xsl:value-of select="."/>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i"/>
<xsl:param name="n"/>
xsl:choose
<xsl:when test="$i = 1 or $i = $n">
xsl:textN</xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$n - 2"/>
</xsl:with-param>
</xsl:call-template>
xsl:textN
</xsl:text>
</xsl:when>
xsl:otherwise
xsl:textN</xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$i - 2"/>
</xsl:with-param>
</xsl:call-template>
xsl:textN</xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$n - $i - 1"/>
</xsl:with-param>
</xsl:call-template>
xsl:textN
</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$i < $n">
<xsl:call-template name="loop">
<xsl:with-param name="i">
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="n">
<xsl:value-of select="$n"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="spaces">
<xsl:param name="n"/>
<xsl:if test="$n > 0">
xsl:text </xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$n - 1"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This
Task 2
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/input">
<xsl:call-template name="loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="n">
<xsl:value-of select="."/>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i"/>
<xsl:param name="n"/>
<xsl:choose>
<xsl:when test="$i = 1 or $i = $n">
<xsl:text>N</xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$n - 2"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text>N </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>N</xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$i - 2"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text>N</xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$n - $i - 1"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text>N </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$i < $n">
<xsl:call-template name="loop">
<xsl:with-param name="i">
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="n">
<xsl:value-of select="$n"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="spaces">
<xsl:param name="n"/>
<xsl:if test="$n > 0">
<xsl:text> </xsl:text>
<xsl:call-template name="spaces">
<xsl:with-param name="n">
<xsl:value-of select="$n - 1"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This one defines 2 recursive templates, loop and spaces. loop with parameters i and n will generate the desired output for n, starting at position i. spaces with parameter n will generate n spaces.
##Task 3
Input
Task 3
Input for this must be in <input><num>..</num><num>..</num></input> tags.