XSL條件IF
XSL可以用一個(gè)IF語句過濾來自XML文檔的信息。
在哪里放置IF條件
現(xiàn)在來重新看看你已經(jīng)看過多次的XML文檔:
以下為引用的內(nèi)容: <?xml version="1.0"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> . . . |
以下為引用的內(nèi)容: <xsl:if match=".[ARTIST='Bob Dylan']"> ... 一些輸出... </xsl:if> |
以下為引用的內(nèi)容: <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="CATALOG/CD"> <xsl:if match=".[ARTIST='Bob Dylan']"> <tr> <td><xsl:value-of select="TITLE"/></td> <td><xsl:value-of select="ARTIST"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
以下是在瀏覽器中將XML文件轉(zhuǎn)換成HTML所需要的簡單代碼:
以下為引用的內(nèi)容: <html> <body> <script language="javascript"> // Load XML var xml = new ActiveXObject("Microsoft.XMLDOM") xml.async = false xml.load("cd_catalog.xml") // Load the XSL var xsl = new ActiveXObject("Microsoft.XMLDOM") xsl.async = false xsl.load("cd_catalog_if.xsl") // Transform document.write(xml.transformNode(xsl)) </script> </body> </html> |
XSL條件選擇Choose
XSL可以使用條件選擇過濾XML文檔。
在哪里放置選擇條件
重新看看幾乎在前面每個(gè)章節(jié)都看到過的XML文檔:
以下為引用的內(nèi)容: <?xml version="1.0"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> . . . |
以下為引用的內(nèi)容: <xsl:choose> <xsl:when match=".[ARTIST='Bob Dylan']"> ... 一些代碼 ... </xsl:when> <xsl:otherwise> ... 一些代碼 ... </xsl:otherwise> </xsl:choose> |
以下為引用的內(nèi)容: <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="CATALOG/CD"> <tr> <td><xsl:value-of select="TITLE"/></td> <xsl:choose> <xsl:when match=".[ARTIST='Bob Dylan']"> <td bgcolor="#ff0000"> <xsl:value-of select="ARTIST"/> </td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="ARTIST"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
以下是在瀏覽器中將XML文件轉(zhuǎn)換成HTML所需要的簡單代碼:
以下為引用的內(nèi)容: <html> <body> <script language="javascript"> // Load XML var xml = new ActiveXObject("Microsoft.XMLDOM") xml.async = false xml.load("cd_catalog.xml") // Load the XSL var xsl = new ActiveXObject("Microsoft.XMLDOM") xsl.async = false xsl.load("cd_catalog_choose.xsl") // Transform document.write(xml.transformNode(xsl)) </script> </body> </html> |
新聞熱點(diǎn)
疑難解答
圖片精選