Выбрать главу

    </TITLE>

   </HEAD>

   <BODY>

    <xsclass="underline" apply-templates select="PLANET"/>

   </BODY>

  </HTML>

 </xsclass="underline" template>

 <xsclass="underline" template match="PLANET">

  <PLANET>

   <xsclass="underline" attribute name="{NAME}">

    <xsclass="underline" value-of select="@COLOR"/>

   </xsclass="underline" attribute>

  </PLANET>

 </xsclass="underline" template>

</xsclass="underline" stylesheet>

Как можно видеть в приведенном ниже результате, я создал новые атрибуты «с ходу», используя названия планет:

<НТМL>

 <HEAD>

  <TITLE>

   Planets

  </TITLE>

 </HEAD>

 <BODY>

  <PLANET Mercury="RED">

  </PLANET>

  <PLANET Venus="WHITE">

  </PLANET>

  <PLANET Earth="BLUE">

  </PLANET>

 </BODY>

</HTML>

Элемент <xsclass="underline" comment>: создание комментариев

По ходу дела можно также создавать и комментарии при помощи элемента <xsclass="underline" comment>. Этот элемент не имеет атрибутов и содержит тело шаблона, задающего текст комментария.

В листинге 6.8 я создаю комментарии для замены элементов <PLANET>; текст комментария включает название планеты.

Листинг 6.8. Применение <xsclass="underline" comment>

<?xml version="1.0"?>

<xsclass="underline" stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsclass="underline" template match="PLANETS">

  <HTML>

   <HEAD>

    <TITLE>

     Planets

    </TITLE>

   </HEAD>

   <BODY>

    <xsclass="underline" apply-templates select="PLANET"/>

   </BODY>

  </HTML>

 </xsclass="underline" template>

 <xsclass="underline" template match="PLANET">

  <xsclass="underline" comment>This was the <xsclass="underline" value-of select="NAME"/> element</xsclass="underline" comment>

 </xsclass="underline" template>

</xsclass="underline" stylesheet>

Вот результат:

<HTML>

 <HEAD>

  <TITLE>

   Planets

  </TITLE>

 </HEAD>

 <BODY>

  <!--This was the Mercury element-->

  <!--This was the Venus element-->

  <!--This was the Earth element-->

 </BODY>

</HTML>

Элемент <xsclass="underline" processing-instruction>: создание инструкций обработки

При помощи элемента <xsclass="underline" processing-instruction> можно создавать новые инструкции обработки. У этого элемента один атрибут:

• name (обязательный). Задает имя инструкции обработки. Принимает значение шаблона значений атрибута, возвращающего NCName.

В следующем примере я удалил инструкцию <?xml-stylesheet?> из начала planets.xmclass="underline"

<?xml version="1.0"?>

<PLANETS>

 <PLANET>

  <NAME>Mercury</NAME>

  <MASS UNITS="(Earth = 1)">.0553</MASS>

  <DAY UNITS="days">58.65</DAY>

  <RADIUS UNITS="miles">1516</RADIUS>

  <DENSITY UNITS="(Earth = 1)">.983</DENSITY>

  <DISTANCE UNITS="million miles">43.4</DISTANCE><!--B перигелии-->

 </PLANET>

 <PLANET>

  <NAME>Venus</NAME>

  <MASS UNITS="(Earth = 1)">.815</MASS>

  <DAY UNITS="days">116.75</DAY>

  <RADIUS UNITS="miles">3716</RADIUS>

  <DENSITY UNITS="(Earth = 1)">.943</DENSITY>

  <DISTANCE UNITS="million miles">66.8</DISTANCE><!--B перигелии-->

 </PLANET>

 .

 .

 .

Для того чтобы снова добавить эту инструкцию обработки, можно применить элемент <xsclass="underline" processing-instruction> (листинг 6.9). Пункты type и href в инструкции обработки, например <?xml-stylesheettype="text/xml" href="planets.xsl"?>, в действительности не являются атрибутами, поэтому их значения нужно устанавливать не при помощи <xsclass="underline" attribute>, а используя простой текст.

Листинг 6.9. Применение <xsclass="underline" processing-instruction>

<?xml version="1.0"?>

<xsclass="underline" stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsclass="underline" output method="xml"/>

 <xsclass="underline" template match="/">

  <xsclass="underline" processing-instruction name="xml-stylesheet">

   <xsclass="underline" text>type="text/xml" href="planets.xsl"</xsclass="underline" text>

  </xsclass="underline" processing-instruction>

  <xsclass="underline" apply-templates/>

 </xsclass="underline" template>

 <xsclass="underline" template match="@*|node()">

  <xsclass="underline" copy>

   <xsclass="underline" apply-templates select="@*|node()"/>

  </xsclass="underline" copy>

 </xsclass="underline" template>

</xsclass="underline" stylesheet>

Вот результат, где инструкция обработки <?xml-stylesheet?> снова на месте:

<?xml version="1.0" encoding="UTF-8?>

<?xml-stylesheet type="text/xml" href="planets.xsl"?>

<PLANETS>

 <PLANET>

  <NAME>Mercury</NAME>

  <MASS UNITS="(Earth = 1)">.0553</MASS>

  <DAY UNITS="days">58.65</DAY>

  <RADIUS UNITS="miles">1516</RADIUS>

  <DENSITY UNITS="(Earth = 1)">.983</DENSITY>

  <DISTANCE UNITS="million miles">43.4</DISTANCE><!--B перигелии-->

 </PLANET>

 <PLANET>

  <NAME>Venus</NAME>

  <MASS UNITS="(Earth = 1)">.815</MASS>

  <DAY UNITS="days">116.75</DAY>

  <RADIUS UNITS="miles">3716</RADIUS>

  <DENSITY UNITS="(Earth = 1)">.943</DENSITY>

  <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->

 </PLANET>

 .

 .

 .

Элемент <xsclass="underline" document>: создание нескольких выходных документов

В рабочем проекте XSLT 1.1 был представлен новым элемент, <xsclass="underline" document>, предназначенный для поддержки нескольких выходных документов, — и, скорее всего, этот элемент будет добавлен в XSLT 2.0. Он имеет следующие атрибуты:

• href (обязательный). Указывает место, в которое должен быть помещен новый документ. Устанавливается в абсолютный или относительный URI, без идентификатора фрагмента;