如何使用XSLT的一个示范(2)

<?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" match="datetime"/>
    <xsl:attribute-set>
        <xsl:attribute>hidden</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set>
        <xsl:attribute>attribute1_value</xsl:attribute>
        <xsl:attribute>attribute2_value</xsl:attribute>
        <xsl:attribute>attribute3_value</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set use-attribute-sets="hidden-regular">
        <xsl:attribute>__VIEWSTATE</xsl:attribute>
        <xsl:attribute>__VIEWSTATE</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set use-attribute-sets="hidden-regular expand-attribute">
        <xsl:attribute>__EVENTARGUMENT</xsl:attribute>
        <xsl:attribute>__EVENTARGUMENT</xsl:attribute>
    </xsl:attribute-set>
    <xsl:template match="to">
        <xsl:variable>group_<xsl:value-of select="myfn:rand()"/>
        </xsl:variable>
        <xsl:value-of select="./text()"/>
        <xsl:if test="group">
            <span>
                &#160;<span alt="Click to Expand">+<xsl:value-of select="@name"/>
                </span>
                <span>
                    <xsl:attribute><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 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 select="@id"/>
        <xsl:variable select="local-name(..)='attachment'"/>
        <xsl:variable>
            <xsl:choose>
                <xsl:when test="$isAttachedMessage">attached_message_outline</xsl:when>
                <xsl:otherwise>message_outline</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable>
            <xsl:choose>
                <xsl:when test="$isAttachedMessage">none</xsl:when>
                <xsl:otherwise>block</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <div>
            <!-- 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 select="body/attachments/attachment/message/@id"/>
                <div>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 use-attribute-sets="viewstate"/>
                <xsl:element use-attribute-sets="event-arguments"/>
                <xsl:apply-templates select="messages"/>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/71cb57c66d08a44ec53c183bdf1ddab0.html