如何使用XSLT的一个示范

写了个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>

扫描二维码关注公众号,回复: 2016064 查看本文章

 这就是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" style="cursor:hand; border:solid 1px black; " onclick="showHide(this,'none');showHide('{$groupId}');">+<xsl:value-of select="@name"/>
                </span>
                <span style="display:none;">
                    <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 style="color:red;">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}" style="display:{$styleDisplay};">
            <!-- Indicates that whether this message below is a attachment -->
            <xsl:if test="$isAttachedMessage">
                <div style="color:red; font-weight:bold;padding:2px; border-bottom:dashed 1px red;">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 style="color:red;">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>

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138303.htm
今日推荐