XML Tutorial/XSLT stylesheet/parameter
Содержание
A way how to recover the value of global variable which has the same name as a local one
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter>
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="/">
<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="."/> |
</xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>First chapter : Chapter A |
Chapter : Chapter B |
Chapter : Chapter C |
Last chapter : Chapter D |
Define parameter
<source lang="xml">
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></source>
If you want to pass a variable, you have to define this variable with xsl:param element.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<number>1</number> <number>3</number> <number>4</number> <number>17</number> <number>8</number>
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="/">
<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> |
1 (odd)</TH> |
<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> |
</xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>Chapter A (1/4) |
Chapter B (2/4) |
Chapter C (3/4) |
Chapter D (4/4) |
template with parameter
<source lang="xml">
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></source>
Variable and parameter
<source lang="xml">
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:</source>