There is XML.

<file> <section> <comment># Секция заслешивания путей без слеша в конце</comment> <rule typename="RewriteRule" pattern="/([-a-zA-Z]+)" substitution="/$1/" flags="R=301,L" /> <rule typename="RewriteRule" pattern="/([-a-zA-Z]+)/([-a-zA-Z]+)" substitution="/$1/$2/" flags="R=301,L" /> <rule typename="RewriteRule" pattern="/([-a-zA-Z]+)/([-a-zA-Z]+)/([-a-zA-Z]+)" substitution="/$1/$2/$3/" flags="R=301,L" /> </section> <section> <comment># Секция преобразования URL в вызов контроллера</comment> </section> </file> 

At the exit after the conversion, we get the following text document.

 # Секция заслешивания путей без слеша в конце RewriteRule ^/([-a-zA-Z]+)/([-a-zA-Z]+)/([-a-zA-Z]+)$ /$1/$2/$3/ [R=301,L] RewriteRule ^/([-a-zA-Z]+)/([-a-zA-Z]+)$ /$1/$2/ [R=301,L] RewriteRule ^/([-a-zA-Z]+)$ /$1/ [R=301,L] # Секция преобразования URL в вызов контроллера 

We have the following template for working with rule nodes

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- шаблон сопоставления с узлом rule --> <xsl:template match="rule"> <!-- Если у узла rule есть хотябы узел типа и он не пустой --> <xsl:if test="@typename != ''"> <!-- Выведем переход строки --> <xsl:text>&#xa;</xsl:text> <!-- Выведем из текущего узла тип правила --> <xsl:value-of select="@typename"></xsl:value-of> <!-- Начинаем паттерн с сопоставления с началом строки --> <xsl:text> ^</xsl:text> <!-- Выведем сам паттерн --> <xsl:value-of select="@pattern"></xsl:value-of> <!-- Завершим сопостовлением с концом строки --> <xsl:text>$ </xsl:text> <!-- Выведем правило подстановки --> <xsl:value-of select="@substitution"></xsl:value-of> <!-- Если есть свойство флагов и оно не пустое --> <xsl:if test="@flags != ''"> <!-- Начнем выводить блок флагов --> <xsl:text> [</xsl:text> <!-- Выведем влаги --> <xsl:value-of select="@flags"></xsl:value-of> <!-- Завершаем вывод флагов --> <xsl:text>]</xsl:text> </xsl:if> </xsl:if> </xsl:template> </xsl:stylesheet> 

The question of how to modify the template so that it would be in each pattern would remove the first slash from the strings like ^/([-a-zA-Z]+)$ and the pattern would look like this ^([-a-zA-Z]+)$

  • Do not forget to specify <xsl:output method="text" /> so that it does not start escaping special characters where it is not necessary. - Pavel Mayorov

1 answer 1

Using functions to work with the text substring() or substring-after() . substring() returns a substring from the source string, starting at the specified position, and substring-after() returns the substring after the specified character

 <xsl:value-of select="substring(@pattern, 2)"/> <xsl:value-of select="substring-after(@pattern, '/')"/> 
  • I take only the first steps in XSLT and the answer did not let me understand how to proceed further .... - Dmitry Gvozd
  • what exactly is not clear? Just replace your <xsl: value-of select = "@ pattern" /> `call with any mine. - Anton Shchyrov
  • well understood thanks. Do you have at hand a reference to a good XSLT reference, preferably in Russian, if you have one? - Dmitriy Gvozd
  • Two duplicate links. One on MSDN msdn.microsoft.com/ru-ru/library/ms256069(v=vs.120).aspx the second on W3Schools w3schools.com/xsl/default.asp . A machine translation (quite adequate) in Russian is available on MSDN - Anton Shchyrov