基于属性的输出值

时间:2013-06-18 12:38:24

标签: xml xslt

有没有办法根据属性输出值?
我对XSLT很新,所以如果很明显,请耐心等待:)

我的XML看起来像这样:

<Columns>
    <Column DataType="String">Id</Column>
    <Column DataType="String">FirstName</Column>
    <Column DataType="String">LastName</Column>
    <Column DataType="String">TheDescription</Column>
</Columns>
<Rows>
    <Row>
        <Value Column="Id">1</Value>
        <Value Column="FirstName">John</Value>
        <Value Column="LastName">Doe</Value>
        <Value Column="TheDescription">Some description about John Doe</Value>
    </Row>
    <Row>
        <Value Column="Id">2</Value>
        <Value Column="FirstName">Jane</Value>
        <Value Column="LastName">Doe</Value>
        <Value Column="TheDescription">Some description about Jane Doe</Value>
    </Row>
</Rows>

我尝试过以下操作:

<xsl:template match="Template">
    <xsl:apply-templates select="loop[@name='Rows']" />
</xsl:template>

<xsl:template match="loop[@name='Rows']">
    <table border="1" cellpadding="2" cellspacing="2">
        <tr>
            <xsl:apply-templates select="../loop[@name='Columns']/item" mode="header" />
        </tr>
        <xsl:apply-templates select="item" mode="row" />
    </table>
</xsl:template>

<xsl:template match="item" mode="row">
    <tr>
        <xsl:apply-templates select="loop[@name='Row']" />
    </tr>
</xsl:template>

<xsl:template match="loop[@name='Row']">
    <xsl:param name="Column" />
    <xsl:if test="$Column = FirstName">
        <td>Output the value of FirstName here!</td>
    </xsl:if>
</xsl:template>

最后6行应该输出FirstName。怎么办?

1 个答案:

答案 0 :(得分:0)

我想你需要这样的东西:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" encoding="iso-8859-1" indent="yes"/>
    <xsl:template match="/*">
        <table>
            <tr>
                <xsl:for-each select="Columns/Column">
                    <th><xsl:value-of select="." /></th>
                </xsl:for-each>
            </tr>
            <xsl:for-each select="Rows/Row">
                <tr>
                    <xsl:for-each select="Value">
                        <td>
                            <xsl:choose>
                                <xsl:when test="@Column = 'FirstName'">
                                    <b><xsl:value-of select="." /></b>
                                </xsl:when>
                                <xsl:when test="@Column = 'LastName'">
                                    <i><xsl:value-of select="." /></i>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="." />
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>