歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 如何使用XSLT的一個示范

如何使用XSLT的一個示范

日期:2017/3/1 9:08:10   编辑:Linux編程

寫了個XSLT小示范,僅希望能給初學者一點參考價值。實例僅為了更多的應用到XSLT的各個元素去表現功能而並非最佳實踐。在讀這篇文章之前你應該就關於XSLT的基礎概念有一些基本了解以及對XPath有比較熟練的掌握,因為XSLT是基於XPath去匹配需要應用的模板以及定位節點等。這篇文章不再贅述,

在這篇實例中主要應用了以下一些常用的XSLT元素,如:<xsl:stylesheet>,<xsl:apply-templates>,<xsl:attribute>,<xsl:attribute-set>,<xsl:choose>,<xsl:comment>,<xsl:copy>,<xsl:element>,<xsl:for-each>,<xsl:if>,<xsl:otherwise>,<xsl:output>,<xsl:sort>,<xsl:template>,<xsl:text>,<xsl:value-of>,<xsl:variable>,<xsl:when>等元素以及顯示怎樣顯示HTML空格(&nbsp;)使用自定義函數等都有涉及。

直接進入主題吧,首先看一下最直觀的顯示輸出吧,如下圖:

這裡是原始的XML文件內容(message.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2008 rel. 2 sp2 (http://www.altova.com) by Administrator -->
<?xml-stylesheet type="text/xsl" href="messages.xsl"?>
<messages>
<message id="1">
<sender>[email protected]</sender>
<to>[email protected]
<group name="IT">
<address>[email protected]</address>
<address>[email protected]</address>
<address>[email protected]</address>
<address>[email protected]</address>
<address>[email protected]</address>
</group>
</to>
<subject>This is a sample</subject>
<datetime date="2008-12-11" time="12:00:00" formatted="12/11/2008 12:00AM">2008-12-11T12:00:00Z</datetime>
<body>
Are you interested in?
<attachments>
<attachment id="1">
<message id="0">
<sender>[email protected]</sender>
<to>[email protected]</to>
<body>
We strongly recommend the following books
<books xmlns:amazon="http://www.amazon.com/books/schema">
<amazon:book>
<name>Professional C# 2008 </name>
<country>USA</country>
<price>37.79</price>
<year>2007</year>
</amazon:book>
<amazon:book>
<name>Microsoft Visual C# 2008 Step by Step </name>
<country>USA</country>
<price>26.39 </price>
<year>2008</year>
</amazon:book>
<amazon:book>
<name>C# in Depth</name>
<country>USA</country>
<price>29.69 </price>
<year>2006</year>
</amazon:book>
<amazon:book>
<name>Thinking in Java</name>
<country>USA</country>
<price>23.69 </price>
<year>2004</year>
</amazon:book>
</books>
</body>
</message>
</attachment>
</attachments>
</body>
</message>
<message id="2">
<sender>[email protected]</sender>
<to>[email protected]</to>
<subject>No title</subject>
<body/>
</message>
</messages>

這就是XSLT文件(message.xsl):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myfn="uri://none" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<msxsl:script language="JavaScript" implements-prefix="myfn"><![CDATA[
function rand()
{
return Math.random().toString().substr(2,8);
}
]]></msxsl:script>
<xsl:output version="4.0"/>
<xsl:key use="@id" name="senddate" match="datetime"/>
<xsl:attribute-set name="hidden-regular">
<xsl:attribute name="type">hidden</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="expand-attribute">
<xsl:attribute name="attribute1">attribute1_value</xsl:attribute>
<xsl:attribute name="attribute2">attribute2_value</xsl:attribute>
<xsl:attribute name="attribute3">attribute3_value</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="viewstate" use-attribute-sets="hidden-regular">
<xsl:attribute name="id">__VIEWSTATE</xsl:attribute>
<xsl:attribute name="name">__VIEWSTATE</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="event-arguments" use-attribute-sets="hidden-regular expand-attribute">
<xsl:attribute name="id">__EVENTARGUMENT</xsl:attribute>
<xsl:attribute name="name">__EVENTARGUMENT</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="to">
<xsl:variable name="groupId">group_<xsl:value-of select="myfn:rand()"/>
</xsl:variable>
<xsl:value-of select="./text()"/>
<xsl:if test="group">
<span class="contact_group">
&#160;<span alt="Click to Expand" onclick="showHide(this,'none');showHide('{$groupId}');">+<xsl:value-of select="@name"/>
</span>
<span >
<xsl:attribute name="id"><xsl:value-of select="$groupId"/></xsl:attribute>
<xsl:for-each select="group/address">
<xsl:value-of select="."/>
<xsl:if test="position()&lt;count(../address)">;</xsl:if>
</xsl:for-each>
</span>
</span>
</xsl:if>
</xsl:template>
<xsl:template match="books">
<table border="1" cellpadding="5" cellspacing="0">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>Country</th>
<th>Price</th>
<th>Year</th>
</tr>
<xsl:for-each select="*[local-name()='book' and ./year>2004]">
<xsl:sort select="year" order="descending"/>
<xsl:sort select="name" case-order="lower-first"/>
<tr>
<td>
<xsl:apply-templates select="name"/>
</td>
<td>
<xsl:value-of select="country"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="year"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="name">
<xsl:value-of select="."/>
<xsl:if test="../year=2008">
<span >new</span>
</xsl:if>
</xsl:template>
<xsl:template match="message">
<xsl:variable name="messageId" select="@id"/>
<xsl:variable name="isAttachedMessage" select="local-name(..)='attachment'"/>
<xsl:variable name="className">
<xsl:choose>
<xsl:when test="$isAttachedMessage">attached_message_outline</xsl:when>
<xsl:otherwise>message_outline</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="styleDisplay">
<xsl:choose>
<xsl:when test="$isAttachedMessage">none</xsl:when>
<xsl:otherwise>block</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<div id="message_{$messageId}" class="{$className}" >
<!-- Indicates that whether this message below is a attachment -->
<xsl:if test="$isAttachedMessage">
<div >Attached Message Preview</div>
</xsl:if>
<table>
<tr>
<td>Sender:</td>
<td>
<xsl:value-of select="sender"/>
</td>
</tr>
<tr>
<td>to:</td>
<td>
<xsl:apply-templates select="to"/>
</td>
</tr>
<tr>
<td>Subject:</td>
<td>
<xsl:value-of select="subject"/>
</td>
</tr>
</table>
<hr/>
<!-- Shows a attachements indicator bar and its content to preview-->
<xsl:if test="./body/attachments/attachment">
<xsl:variable name="attachedMessageId" select="body/attachments/attachment/message/@id"/>
<div class="attachment_indicator_bar" onclick="showHide('message_{$attachedMessageId}');">This message contains one or more attachment(s)
</div>
<div>
<!--<xsl:copy>
<xsl:apply-templates></xsl:apply-templates>
</xsl:copy>-->
</div>
<xsl:apply-templates select="body/attachments/attachment/message"/>
<br/>
</xsl:if>
<!-- Shows message body -->
<div>
<xsl:apply-templates select="body"/>
</div>
</div>
</xsl:template>
<xsl:template match="body">
<xsl:comment xml:space="preserve">The body of a message</xsl:comment>
<div>
<xsl:value-of select="./text()"/>
<br/>
<br/>
<xsl:copy>
<xsl:if test=".//books and not(./books)">
<div >The attachment was sent by : <xsl:value-of select=".//message//child::node()"/>
</div>
</xsl:if>
</xsl:copy>
<xsl:if test="./books">
<xsl:apply-templates select="./books"/>
</xsl:if>
</div>
</xsl:template>
<xsl:template match="messages">
<xsl:for-each select="./message">
<div>
<xsl:apply-templates select="."/>
</div>
</xsl:for-each>
</xsl:template>
<xsl:template match="/">
<html>
<head>
<title>Messages Demo</title>
<style type="text/css">
body{ font-size:9px; font-family:verdana; }
table{ font-size:9px; font-family:verdana; }
div.message_outline { border:dashed 1px #9acd32;margin-bottom:10px; padding:5px;}
div.attached_message_outline { border:solid 1px orange; padding:5px;}
div.attachment_indicator_bar {background-color:orange; padding:3px; font-weight:bold; cursor:hand;}
div.attachment_preview_outline{}
table.book{}
</style>
<script type="text/javascript" language="javascript"><![CDATA[
function $(element) {
if (isNull(element)) {
return null;
} else if (typeof element == "object") {
return element;
} else {
return document.getElementById(element);
}
}
function isNull(o) {
return o == null || o == 'undefined' || o == 'unknown';
}
function showHide(element, sDisplay) {
var oElement = $(element);
if (!isNull(element)) {
if (!isNull(sDisplay)) {
oElement.style.display = sDisplay;
} else {
oElement.style.display = (oElement.style.display == "" || oElement.style.display == "none") ? "block" : "none";
}
}
}
]]></script>
</head>
<body>
<xsl:element name="input" use-attribute-sets="viewstate"/>
<xsl:element name="input" use-attribute-sets="event-arguments"/>
<xsl:apply-templates select="messages"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Copyright © Linux教程網 All Rights Reserved