<xsl:param name='alpha' select='30'/>

 <xsl:variable name='alpha-radian' select='3.14 * ($alpha div 180)'/>

 <xsl:template match='/'>

  <svg width='200' height='200'>

   <desc>Simple line-based figure</desc>

   <xsl:apply-templates select='точки'/>

  </svg>

 </xsl:template>

 <xsl:template match='точки'>

  <g style='stroke:black; stroke-width:2'>

   <xsl:apply-templates select='точка'/>

  </g>

 </xsl:template>

 <xsl:template match='точка'>

  <xsl:variable name='x1' select='@x'/>

  <xsl:variable name='y1' select='@y'/>

  <xsl:variable name='x2r'>

   <xsl:choose>

    <xsl:when test='position() = last()'>

     <xsl:value-of select='preceding-sibling::точка[last()]/@x'/>

    </xsl:when>

    <xsl:otherwise>

     <xsl:value-of select='following-sibling::точка[1]/@x'/>

    </xsl:otherwise>

   </xsl:choose>

  </xsl:variable>

  <xsl:variable name='y2r'>

   <xsl:choose>

    <xsl:when test='position() = last()'>

     <xsl:value-of select='preceding-sibling::точка[last()]/@y'/>

    </xsl:when>

    <xsl:otherwise>

     <xsl:value-of select='following-sibling::точка[1]/@y'/>

    </xsl:otherwise>

   </xsl:choose>

  </xsl:variable>

  <xsl:variable name='x2' select='number($x2r)'/>

  <xsl:variable name='y2' select='number($y2r)'/>

  <line

   x1='{$x1 * math:cos($alpha-radian) -

        $y1 * math:sin($alpha-radian) + 100}'

   y1='{$x1 * math:sin($alpha-radian) +

        $y1 * math:cos($alpha-radian) + 100}'

   x2='{$x2 * math:cos($alpha-radian) -

        $y2 * math:sin($alpha-radian) + 100}'

   y2='{$x2 * math:sin($alpha-radian) +

        $y2 * math:cos($alpha-radian) + 100}'/>

 </xsl:template>

</xsl:stylesheet>

Результатом этого преобразования будет следующий документ.

Листинг 10.7. Результирующий SVG-документ

<!DOCTYPE svg

 PUBLIC '-//W3C//DTD SVG 1.0//EN'

 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>

<svg

 xmlns='http://www.w3.org/2000/svg'

 xmlns:math='java:java.lang.Math'

 width='200'

 height='200'>

 <desc>Simple line-based figure</desc>

  <g style='stroke:black; stroke-width:2'>

  <line

   x1='81.68060041188197' y1='31.70359014757173'

   x2='168.29640985242827' y2='81.68060041188197'/>

  <line

   x1='168.29640985242827' y1='81.68060041188197'

   x2='118.31939958811803' y2='168.29640985242827'/>

  <line

   x1='118.31939958811803' y1='168.29640985242827'

   x2='31.70359014757173' y2='118.31939958811803'/>

  <line

   x1='31.70359014757173' y1='118.31939958811803'

   x2='81.68060041188197' y2='31.70359014757173'/>

 </g>

</svg>

Визуальное представление этого документа демонстрирует рис. 10.2, где представлен поворот, выполненный на 30°:

Рис. 10.2. Визуальное представление полученного SVG-документа

Анализируя полученный документ, мы можем заметить объявление пространства имен с префиксом math, которое было в него включено:

<svg

 xmlns='http://www.w3.org/2000/svg'

 xmlns:math='java:java.lang.Math'

 width='200'

 height='200'>

 ...

Это тот самый случай, когда объявление пространства имен используется в самом преобразовании, но является лишним в выходящем документе. Для того чтобы избавиться от него, нужно просто включить префикс math в атрибут exclude-result-prefixes элемента xsl:stylesheet.

<xsl:stylesheet

Вы читаете Технология XSLT
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату