Skip to content

Instantly share code, notes, and snippets.

@maximzasorin
Last active September 27, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximzasorin/c9b165c8e7c09de87df9d83366c37735 to your computer and use it in GitHub Desktop.
Save maximzasorin/c9b165c8e7c09de87df9d83366c37735 to your computer and use it in GitHub Desktop.
Шаблон для склонения существительных в XSL
<xsl:template name="declension1">
<xsl:param name="number" />
<xsl:variable name="nominative">
<xsl:text>слово</xsl:text>
</xsl:variable>
<xsl:variable name="genitive_singular">
<xsl:text>слова</xsl:text>
</xsl:variable>
<xsl:variable name="genitive_plural">
<xsl:text>слов</xsl:text>
</xsl:variable>
<xsl:variable name="last_digit">
<xsl:value-of select="$number mod 10"/>
</xsl:variable>
<xsl:variable name="last_two_digits">
<xsl:value-of select="$number mod 100"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$last_digit = 1 and $last_two_digits != 11">
<xsl:value-of select="$nominative"/>
</xsl:when>
<xsl:when test="$last_digit = 2 and $last_two_digits != 12 or $last_digit = 3 and $last_two_digits != 13 or $last_digit = 4 and $last_two_digits != 14">
<xsl:value-of select="$genitive_singular"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$genitive_plural"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="declension1_wrapper">
<xsl:param name="number" />
<xsl:value-of select="$number" />
<xsl:text> </xsl:text>
<xsl:call-template name="declension1">
<xsl:with-param name="number" select="$number"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="declension2">
<xsl:param name="number" />
<xsl:param name="word" />
<xsl:variable name="nominative">
<xsl:value-of select="substring-before($word, '|')" />
</xsl:variable>
<xsl:variable name="genitive_singular">
<xsl:value-of select="substring-before(substring-after($word, '|'), '|')" />
</xsl:variable>
<xsl:variable name="genitive_plural">
<xsl:value-of select="substring-after(substring-after($word, '|'), '|')" />
</xsl:variable>
<xsl:variable name="last_digit">
<xsl:value-of select="$number mod 10"/>
</xsl:variable>
<xsl:variable name="last_two_digits">
<xsl:value-of select="$number mod 100"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$last_digit = 1 and $last_two_digits != 11">
<xsl:value-of select="$nominative"/>
</xsl:when>
<xsl:when test="$last_digit = 2 and $last_two_digits != 12 or $last_digit = 3 and $last_two_digits != 13 or $last_digit = 4 and $last_two_digits != 14">
<xsl:value-of select="$genitive_singular"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$genitive_plural"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="declension2_wrapper">
<xsl:param name="number" />
<xsl:param name="word" />
<xsl:value-of select="$number" />
<xsl:text> </xsl:text>
<xsl:call-template name="declension2">
<xsl:with-param name="number" select="$number"/>
<xsl:with-param name="word" select="$word"/>
</xsl:call-template>
</xsl:template>
<xsl:variable name="count" select="2" />
<!-- Если слово повторяется в шаблоне несколько раз,
то удобно определить такой подшаблон один раз и затем вызывать его
-->
<xsl:value-of select="$count" />
<xsl:text> </xsl:text>
<xsl:call-template name="declension1">
<xsl:with-param name="number" select="$count"/>
</xsl:call-template>
<!-- или -->
<xsl:call-template name="declension1_wrapper">
<xsl:with-param name="number" select="$count"/>
</xsl:call-template>
<br />
<br />
<!--
Если слово повторяется только один раз,
то удобно использовать такой подшаблон
-->
<xsl:value-of select="$count" />
<xsl:text> </xsl:text>
<xsl:call-template name="declension2">
<xsl:with-param name="number" select="$count"/>
<xsl:with-param name="word" select="'слово|слова|слов'"/>
</xsl:call-template>
<!-- или -->
<xsl:call-template name="declension2_wrapper">
<xsl:with-param name="number" select="$count"/>
<xsl:with-param name="word" select="'слово|слова|слов'"/>
</xsl:call-template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment