<xsclass="underline" copy>
<xsclass="underline" attribute name="element-count">
<xsclass="underline" value-of select="count(*) "/>
</xsclass="underline" attribute>
<xsclass="underline" apply-templates select="@*|node()"/>
</xsclass="underline" copy>
</xsclass="underline" template>
<a element-count="3">
text
<b element-count="0" attr="value"/>
<c element-count="0"/>
<d element-count="1">
text
<e element-count="0"/>
</d>
</a>
Если xsclass="underline" copy используется для создания в выходящем документе копии узла элемента, в него при помощи атрибута use-attribute-sets могут быть также включены именованные наборы атрибутов (см. раздел "Именованные наборы атрибутов" данной главы).
Предыдущее преобразование может быть переписано в виде
<xsclass="underline" attribute-set name="elements">
<xsclass="underline" attribute name="element-count">
<xsclass="underline" value-of select="count(*)"/>
</xsclass="underline" attribute>
</xsclass="underline" attribute-set>
<xsclass="underline" template match="@*|node()">
<xsclass="underline" copy use-attribute-sets="elements">
<xsclass="underline" apply-templates select="@*|node()"/>
</xsclass="underline" copy>
</xsclass="underline" template>
Результат преобразования будет абсолютно идентичен выходящему документу, полученному в предыдущем примере.
Элемент xsclass="underline" copy-of
Синтаксис элемента несложен:
<xsclass="underline" copy-of
select="выражение"/>
Использование элемента xsclass="underline" copy-of полностью аналогично использованию элемента xsclass="underline" value-of за тем исключением, что xsclass="underline" copy-of при выводе значения выражения преобразует его к строке не во всех случаях. Поведение xsclass="underline" copy-of зависит от того, какой тип данных возвращает выражение.
□ Если результат вычисления имеет булевый, числовой или строковый тип, то xsclass="underline" copy-of выводит его в виде текстового узла. В этом случае поведение xsclass="underline" copy-of абсолютно не отличается от поведения элемента xsclass="underline" value-of.
□ Если результатом вычисления выражения является множество узлов (node-set), то xsclass="underline" copy-of копирует в выходящий документ все узлы в порядке просмотра документа вместе с их потомками.
□ Если результатом вычисления является результирующий фрагмент дерева, то он копируется в выходящий документ в неизмененном виде.
Рассмотрим пример.
<values>
<boolean>false</boolean>
<string>text</string>
<number>3.14</number>
<node-set>
<item>10</item>
<item>20</item>
<item>30</item>
</node-set>
<tree>
text
<root>
text
<branch>
text
<leaf/>
<leaf/>
</branch>
<leaf/>
</root>
</tree>
</values>
<xsclass="underline" stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsclass="underline" template match="/">
<xsclass="underline" variable name="boolean" select="values/boolean='true'"/>
<xsclass="underline" variable name="string" select="string(values/string)"/>
<xsclass="underline" variable name="number" select="number(values/number)"/>
<xsclass="underline" variable name="node-set" select="values/node-set/*"/>
<xsclass="underline" variable name="tree">
<xsclass="underline" copy-of select="values/tree/*"/>
</xsclass="underline" variable>
<xsclass="underline" text> Value-of boolean:</xsclass="underline" text>
<xsclass="underline" value-of select="$boolean"/>
<xsclass="underline" text> Copy-of boolean:</xsclass="underline" text>
<xsclass="underline" copy-of select="$boolean"/>
<xsclass="underline" text> Value-of string:</xsclass="underline" text>
<xsclass="underline" value-of select="$string"/>
<xsclass="underline" text> Copy-of string:</xsclass="underline" text>
<xsclass="underline" copy-of select="$string"/>
<xsclass="underline" text> Value-of number:</xsclass="underline" text>
<xsclass="underline" value-of select="$number"/>
<xsclass="underline" text> Copy-of number:</xsclass="underline" text>
<xsclass="underline" copy-of select="$number"/>
<xsclass="underline" text> Value-of node-set:</xsclass="underline" text>
<xsclass="underline" value-of select="$node-set"/>
<xsclass="underline" text> Copy-of node-set:</xsclass="underline" text>
<xsclass="underline" copy-of select="$node-set"/>
<xsclass="underline" text> Value-of tree:</xsclass="underline" text>
<xsclass="underline" value-of select="$tree"/>
<xsclass="underline" text> Copy-of tree:</xsclass="underline" text>
<xsclass="underline" copy-of select="$tree"/>
</xsclass="underline" template>
</xsclass="underline" stylesheet>
Value-of boolean:false
Copy-of boolean:false
Value-of string:text
Copy-of string:text
Value-of number:3.14
Copy-of number:3.14
Value-of node-set:10
Copy-of node-set:<item>10</item><item>20</item><item>30</item>
Value-of tree:
text
text
Copy-of tree:<root>
text
<branch>
text
<leaf/>
<leaf/>
</branch>
<leaf/>
</root>
Условная обработка
В XSLT имеются две инструкции, которые поддерживают условную обработку — xsclass="underline" if и xsclass="underline" choose. Инструкция xsclass="underline" if позволяет создавать простые условия типа "если-то", в то время как xsclass="underline" choose создает более сложную конструкцию для выбора одной из нескольких имеющихся возможностей в зависимости от выполнения тех или иных условий.