开发者

XSLT maintain tag order when formatting an xml doc

The problem is that in the XSLT doc below, all my hrow tags are at the bottom, I want it to maintain the order in which they appear, how do I do that?

my xml doc

<log>
<hrow time="45:43:2343">A heading</hrow>
<row type="e">An error</row>
<row type="w">An warn</row>
<row type="i">An info</row>
<row type="d">An debug</row>
<row type="t">unknown</row>
<hrow time="45:43:2343">Another heading</hrow>
<row type="t">more rows</row>
</log>

the xslt doc

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www开发者_Python百科.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <table width="100%">
            <xsl:apply-templates />
        </table>
    </xsl:template>

    <xsl:template match="log">
            <xsl:apply-templates select="row" />
            <xsl:apply-templates select="hrow" />
    </xsl:template>

    <xsl:template match="row">
        <xsl:variable name="type" select="@type" />
        <xsl:choose>
            <xsl:when test="$type = 'd'">
                <tr>
                    <td style="background-color:#C6F98B">
                        <xsl:value-of select="." />
                    </td>
                </tr>
            </xsl:when>
            <xsl:when test="$type = 'i'">
                <tr>
                    <td style="background-color:#8B8BF9">
                        <xsl:value-of select="." />
                    </td>
                </tr>
            </xsl:when>
            <xsl:when test="$type = 'e'">
                <tr>
                    <td style="background-color:#F9555D">
                        <xsl:value-of select="." />
                    </td>
                </tr>
            </xsl:when>
            <xsl:when test="$type = 'w'">
                <tr>
                    <td style="background-color:#F8F781">
                        <xsl:value-of select="." />
                    </td>
                </tr>
            </xsl:when>
            <xsl:otherwise>
                <tr>
                    <td style="background-color:#E4E4E4">
                        <xsl:value-of select="." />
                    </td>
                </tr>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="hrow">
        <tr>
            <td style="background-color:#DBC5FF;font-size: 16px;">
                <xsl:value-of select="." />
                [
                <xsl:value-of select="@time" />
                ]
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>


With the posted XML document, instead of

<xsl:template match="log">
        <xsl:apply-templates select="row" />
        <xsl:apply-templates select="hrow" />
</xsl:template>

simply do

<xsl:template match="log">
        <xsl:apply-templates/>
</xsl:template>

that processes all child nodes of the log element in document order.

Or if you have other child nodes not shown but which you don't want to process then use

<xsl:template match="log">
        <xsl:apply-templates select="row | hrow"/>
</xsl:template>

that way the selected elements (i.e. row and hrow) are processed in document order too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜