XML Tutorial/XSLT stylesheet/call template

Материал из Web эксперт
Версия от 11:27, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Call defined template

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="US-ASCII"?> <state name="BC">

<county>Bristol<population>1</population></county>

<county>Kent</county>
<county>Newport</county>
<county>Providence</county>
<county>Washington</county>

</state>

File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="state">
   <xsl:call-template name="nl" />
   <xsl:text>Counties of </xsl:text>
   <xsl:value-of select="@name" />
   <xsl:call-template name="nl" />
   <xsl:text>Description: </xsl:text>
   <xsl:value-of select="description" />
   <xsl:call-template name="nl" />
   <xsl:text>Source: </xsl:text>
   <xsl:value-of select="from" />
   <xsl:call-template name="nl" />
   <xsl:text>URL: </xsl:text>
   <xsl:value-of select="url" />
   <xsl:call-template name="nl" />
   <xsl:apply-templates select="county" />
   <xsl:text>Estimated state population: </xsl:text>
   <xsl:value-of select="sum(county/population)" />
   <xsl:call-template name="nl" />
 </xsl:template>
 <xsl:template match="county">
   <xsl:text> - </xsl:text>
   <xsl:value-of select="@name" />
   <xsl:text>: </xsl:text>
   <xsl:value-of select="population" />
   <xsl:apply-templates select="population" />
 </xsl:template>
 <xsl:template match="population" name="nl">
   <xsl:text>
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output:

Counties of BC Description: Source: URL:

- : 1
- :  - :  - :  - : Estimated state population: 1</source>
   
  

Return value from template

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <list xml:lang="en">

 <title>title 1</title>
 <listitem>item 1</listitem>
 <listitem>item 2</listitem>
 <listitem>item 3</listitem>
 <listitem xml:lang="sw">item 4</listitem>
 <listitem xml:lang="en-gb">item 5</listitem>
 <listitem xml:lang="zu">item 6</listitem>
 <listitem xml:lang="jz">item 7</listitem>

</list> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="/">
   <xsl:text>Here is a list:
</xsl:text>
   <xsl:variable name="listitems" select="list/listitem"/>
   <xsl:call-template name="processListitems">
     <xsl:with-param name="items" select="$listitems"/>
   </xsl:call-template>
 </xsl:template>
 <xsl:template name="processListitems">
   <xsl:param name="items"/>
   <xsl:for-each select="$items">
     <xsl:value-of select="position()"/>
     <xsl:text>.  </xsl:text>
     <xsl:value-of select="."/>
     <xsl:text>
</xsl:text>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet> Output: Here is a list: 1. item 1 2. item 2 3. item 3 4. item 4 5. item 5 6. item 6 7. item 7</source>


use parameter with template

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <province name="BigCity">

<city>city 1</city>
<city>city 2</city>
<city>city 3</city>
<city>city 4</city>
<city>city 5</city>
<city>city 6</city>
<city>city 7</city>
<city>city 8</city>
<city>city 9</city>
<city>city 10</city>
<city>city 11</city>

</province>

File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="province">
   <xsl:text>BigCity Cities</xsl:text>
   <xsl:call-template name="nl">
     <xsl:with-param name="nl" select=""

"" />
   </xsl:call-template>
   <xsl:apply-templates select="city" />
 </xsl:template>
 <xsl:template match="city">
   <xsl:text> -> </xsl:text>
   <xsl:value-of select="." />
   <xsl:call-template name="nl">
     <xsl:with-param name="nl" select=""
"" />
   </xsl:call-template>
 </xsl:template>
 <xsl:template match="city[.="city 4"]">
   <xsl:text> -> </xsl:text>
   <xsl:value-of select="." />
   <xsl:call-template name="nl">
     <xsl:with-param name="nl"
       select="" (second largest city in the Yukon)
"" />
   </xsl:call-template>
 </xsl:template>
 <xsl:template match="city[.="city 11"]">
   <xsl:text> -> </xsl:text>
   <xsl:value-of select="." />
   <xsl:call-template name="nl">
     <xsl:with-param name="nl"
       select="" (largest city in the Yukon)
"" />
   </xsl:call-template>
 </xsl:template>
 <xsl:template name="nl">
   <xsl:param name="nl" />
   <xsl:value-of select="$nl" />
 </xsl:template>

</xsl:stylesheet> Output: BigCity Cities

-> city 1
-> city 2
-> city 3
-> city 4 (second largest city in the Yukon)
-> city 5
-> city 6
-> city 7
-> city 8
-> city 9
-> city 10
-> city 11 (largest city in the Yukon)</source>