XSLT <xsl:choose> एलीमेंट

XSLT <xsl:choose> एलीमेंट बहुपरिस्थिति परीक्षण को व्यक्त करने के लिए <xsl:when> और <xsl:otherwise> को जोड़ता है。

<xsl:choose> एलीमेंट

व्याकरण

<xsl:choose>
  <xsl:when test="expression">
    ... आउटपुट ...
  
  
    ... आउटपुट ....
  

चयन कीयोग्य स्थान

XML फ़ाइल के लिए बहुपरिस्थिति परीक्षण जोड़ने के लिए <xsl:choose>、<xsl:when> और <xsl:otherwise> जोड़ें:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td bgcolor="#ff00ff">
            
          
          
            
          
        
      
      
    
  
  


ऊपरी कोड CD की कीमत 10 से अधिक होने पर "Artist" स्तम्भ को गुलाबी पृष्ठभूमि रंग जोड़ेगा。

上面的转换结果类似这样:

इस XML फ़ाइल को देखेंइस XSL फ़ाइल को देखें查看结果

एक अन्य उदाहरण

यह एक अन्य <xsl:when> एलीमेंट वाला उदाहरण है:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td bgcolor="#ff00ff">
            
          
          <xsl:when test="price > 9">
            
            
          
          
            
          
        
      
      
    
  
  


上面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色,并在 CD 的价格高于 9 且低于等于 10 时向 "Artist" 列添加灰色的背景颜色。

上面的转换结果类似这样:

查看此XML文件查看此XSL文件查看结果