XML Tutorial/XSLT stylesheet/parameter — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:26, 26 мая 2010

A way how to recover the value of global variable which has the same name as a local one

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <chapter>Chapter A</chapter>
    <chapter>Chapter B</chapter>
    <chapter>Chapter C</chapter>
    <chapter>Chapter D</chapter>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="text">Chapter</xsl:variable>
    <xsl:template match="/">
      <TABLE>
        <xsl:for-each select="//chapter">
          <TR>
            <TD>
              <xsl:variable name="text">
                <xsl:choose>
                  <xsl:when test="position() = 1">First chapter</xsl:when>
                  <xsl:when test="position()=last()">Last chapter</xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$text"/>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:value-of select="$text"/>
              <xsl:text> : </xsl:text>
              <xsl:value-of select="."/>
            </TD>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>First chapter : Chapter A</TD></TR><TR><TD>Chapter : Chapter B</TD></TR><TR><TD>Chapter : Chapter C</TD></TR><TR><TD>Last chapter : Chapter D</TD></TR></TABLE>


Define parameter

File: Data.xml
<?xml version="1.0"?>
<!DOCTYPE catalog SYSTEM "price.dtd">
<catalog>
 <item id="SC-0001">
  <maker>A</maker>
  <description>W</description>
  <size>L</size>
  <price>120.00</price>
  <currency>USD</currency>
 </item>
</catalog>

File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:output doctype-system="catalog.dtd" />
  <xsl:param name="discount" select="0.10" />
  <xsl:template match="catalog">
    <xsl:copy>
      <xsl:apply-templates select="item" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="item">
    <xsl:copy>
      <xsl:attribute name="id"><xsl:value-of select="@id" />
      </xsl:attribute>
      <xsl:copy-of select="maker|description|size|price" />
      <discount>
        <xsl:value-of select="$discount" />
      </discount>
      <discountPrice>
        <xsl:value-of select="price - (price * $discount)" />
      </discountPrice>
      <xsl:copy-of select="currency" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog
  SYSTEM "catalog.dtd">
<catalog>
   <item id="SC-0001">
      <maker>A</maker>
      <description>W</description>
      <size>L</size>
      <price>120.00</price>
      <discount>0.1</discount>
      <discountPrice>108</discountPrice>
      <currency>USD</currency>
   </item>
</catalog>


If you want to pass a variable, you have to define this variable with xsl:param element.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <number>1</number>
    <number>3</number>
    <number>4</number>
    <number>17</number>
    <number>8</number>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE>
        <xsl:for-each select="//number">
          <TR>
            <td>
              <xsl:choose>
                <xsl:when test="text() mod 2">
                  <xsl:apply-templates select=".">
                    <xsl:with-param name="type">odd</xsl:with-param>
                  </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="."/>
                </xsl:otherwise>
              </xsl:choose>
            </TH>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
    <xsl:template match="number">
      <xsl:param name="type">even</xsl:param>
      <xsl:value-of select="."/>
      <xsl:text> (</xsl:text>
      <xsl:value-of select="$type"/>
      <xsl:text>)</xsl:text>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><td>1 (odd)</TH></TR><TR><td>3 (odd)</TH></TR><TR><td>4 (even)</TH></TR><TR><td>17 (odd)</TH></TR><TR><td>8 (even)</TH></TR></TABLE>


setting xsl:param

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <chapter>Chapter A</chapter>
    <chapter>Chapter B</chapter>
    <chapter>Chapter C</chapter>
    <chapter>Chapter D</chapter>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="totalChapters" select="count(//chapter)"/>
    <xsl:template match="/">
      <TABLE>
        <xsl:for-each select="//chapter">
          <TR>
            <TD>
              <xsl:value-of select="."/>
              <xsl:text> (</xsl:text>
              <xsl:value-of select="position()"/>
              <xsl:text>/</xsl:text>
              <xsl:value-of select="$totalChapters"/>
              <xsl:text>)</xsl:text>
            </TD>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>Chapter A (1/4)</TD></TR><TR><TD>Chapter B (2/4)</TD></TR><TR><TD>Chapter C (3/4)</TD></TR><TR><TD>Chapter D (4/4)</TD></TR></TABLE>


template with parameter

File: Data.xml
<?xml version="1.0"?>
<catalog>
 <item id="SC-0001">
  <maker>factory</maker>
  <description>car part</description>
  <size>L</size>
  <price>120.00</price>
  <currency>USD</currency>
 </item>
</catalog>

File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:output doctype-system="catalog.dtd" />
  <xsl:template match="catalog">
    <xsl:copy>
      <xsl:apply-templates select="item">
        <xsl:with-param name="discount" select=""0.50"" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="item">
    <xsl:param name="discount" />
    <xsl:copy>
      <xsl:attribute name="id"><xsl:value-of select="@id" />
      </xsl:attribute>
      <xsl:copy-of select="maker|description|size|price" />
      <discount>
        <xsl:value-of select="$discount" />
      </discount>
      <discountPrice>
        <xsl:value-of select="price - (price * $discount)" />
      </discountPrice>
      <xsl:copy-of select="currency" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog
  SYSTEM "catalog.dtd">
<catalog>
   <item id="SC-0001">
      <maker>factory</maker>
      <description>car part</description>
      <size>L</size>
      <price>120.00</price>
      <discount>0.50</discount>
      <discountPrice>60</discountPrice>
      <currency>USD</currency>
   </item>
</catalog>


Variable and parameter

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:param name="startX" />
  <xsl:param name="baseColor" />
  <xsl:variable name="newline">
    <xsl:text></xsl:text>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:value-of select="$newline" />
    <xsl:text>Global parameters example</xsl:text>
    <xsl:value-of select="$newline" />
    <xsl:value-of select="$newline" />
    <xsl:text>The value of startX is: </xsl:text>
    <xsl:value-of select="$startX" />
    <xsl:value-of select="$newline" />
    <xsl:text>The value of baseColor is: </xsl:text>
    <xsl:value-of select="$baseColor" />
    <xsl:value-of select="$newline" />
  </xsl:template>
</xsl:stylesheet>
Output:
Global parameters exampleThe value of startX is: The value of baseColor is: