Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

1 of 5
LegionMammal978
  • 18.2k
  • 4
  • 25
  • 58

#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 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"> N <xsl:call-template name="spaces"><xsl:with-param name="n"><xsl:value-of select="$n - 2"/></xsl:with-param></xsl:call-template> N </xsl:when> xsl:otherwise N <xsl:call-template name="spaces"><xsl:with-param name="n"><xsl:value-of select="$i - 2"/></xsl:with-param></xsl:call-template> N <xsl:call-template name="spaces"><xsl:with-param name="n"><xsl:value-of select="$n - $i - 1"/></xsl:with-param></xsl:call-template> N </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: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 for this will have to be in <input><num>..</num><num>..</num></input> tags.

<?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="gcd">
 <xsl:with-param name="a"><xsl:value-of select="num[1]"/></xsl:with-param>
 <xsl:with-param name="b"><xsl:value-of select="num[2]"/></xsl:with-param>
 </xsl:call-template>
 </xsl:template>
 <xsl:template name="gcd">
 <xsl:param name="a"/>
 <xsl:param name="b"/>
 <xsl:choose>
 <xsl:when test="$a = 0"><xsl:value-of select="$b"/></xsl:when>
 <xsl:otherwise>
 <xsl:call-template name="gcd">
 <xsl:with-param name="a"><xsl:value-of select="b"/></xsl:with-param>
 <xsl:with-param name="b"><xsl:value-of select="a mod b"/></xsl:with-param>
 </xsl:call-template>
 </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

This one is just a recursive template gcd that uses the Euclidean algorithm.

LegionMammal978
  • 18.2k
  • 4
  • 25
  • 58

AltStyle によって変換されたページ (->オリジナル) /