当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > <xsl:choose>
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.
<xsl:choose>元素通过<xsl:when>和<xsl:otherwise>一起配合使用来表达多种条件语句。
<xsl:choose> |
To insert a multiple conditional test against the XML file, add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
如果你希望向XML文件中插入一个多重条件语句,可以在XSL文件中添加<xsl:choose>,<xsl:when>以及<xsl:otherwise>元素:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> |
The code above will add a pink background-color to the "Artist" column WHEN the price of the CD is higher than 10.
当CD的价格高于10 时,上述代码会将红色背景色添加到对应的”Artist”专栏。
The result of the transformation will look like this:
转换结果如下:
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
Greatest Hits | Dolly Parton |
Still got the blues | Gary Moore |
Eros | Eros Ramazzotti |
One night only | Bee Gees |
Sylvias Mother | Dr.Hook |
Maggie May | Rod Stewart |
Romanza | Andrea Bocelli |
When a man loves a woman | Percy Sledge |
Black angel | Savage Rose |
1999 Grammy Nominees | Many |
For the good times | Kenny Rogers |
Big Willie style | Will Smith |
Tupelo Honey | Van Morrison |
Soulsville | Jorn Hoel |
The very best of | Cat Stevens |
Stop | Sam Brown |
Bridge of Spies | T`Pau |
Private Dancer | Tina Turner |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
The dock of the bay | Otis Redding |
Picture book | Simply Red |
Red | The Communards |
Unchain my heart | Joe Cocker |
View the XML file, View the XSL file, and View the result
XML文件, XSL文件, 以及 最终结果
Here is another example that contains two <xsl:when> elements:
这里举出另外一个案例,其中包含2个<xsl:when>元素:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> |
The code above will add a pink background color to the "Artist" column WHEN the price of the CD is higher than 10, and a grey background-color WHEN the price of the CD is higher than 9 and lower or equal to 10.
当CD的价格高于10时,上面的这些代码会将红色背景色添加到对应的“Artist”专栏;当CD的价格大于9小于等于10时,会将灰色背景色添加到对应的“Artist”专栏。
The result of the transformation will look like this:
转换结果如下:
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
Greatest Hits | Dolly Parton |
Still got the blues | Gary Moore |
Eros | Eros Ramazzotti |
One night only | Bee Gees |
Sylvias Mother | Dr.Hook |
Maggie May | Rod Stewart |
Romanza | Andrea Bocelli |
When a man loves a woman | Percy Sledge |
Black angel | Savage Rose |
1999 Grammy Nominees | Many |
For the good times | Kenny Rogers |
Big Willie style | Will Smith |
Tupelo Honey | Van Morrison |
Soulsville | Jorn Hoel |
The very best of | Cat Stevens |
Stop | Sam Brown |
Bridge of Spies | T`Pau |
Private Dancer | Tina Turner |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
The dock of the bay | Otis Redding |
Picture book | Simply Red |
Red | The Communards |
Unchain my heart | Joe Cocker |
View the XML file, View the XSL file, and View the result
XML文件, XSL文件, 以及 最终结果