XSLT:无法选择属性

时间:2012-07-03 17:29:19

标签: xml xslt

我正在使用XSLT 1.0编写简单的转换并得到奇怪的结果。看起来我不明白,但我发现由于某种原因我无法选择节点的属性。这是我的输入XML:

    <?xml version="1.0"?>
<weekreport>
    <employee name="Emp1">
        <day date="25.06.2012">
            <entry>
                <project>Proj1</project>
                <time>08:00</time>
                <description>Bla-bla-bla</description>
            </entry>
        </day>
    </employee>
    <employee name="Emp2">
        <day date="25.06.2012">
            <entry>
                <project>Proj2</project>
                <time>08:00</time>
                <description></description>
            </entry>
        </day>
    </employee>
</weekreport>

这是XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="entry" name="entry_t">
        <xsl:param name="name"/>
        <xsl:param name="date"/>
        <Row>
            <Cell><xsl:value-of select="$date"/></Cell>
            <Cell><xsl:value-of select="$name"/></Cell>
            <Cell><xsl:value-of select="time"/></Cell>
            <Cell><xsl:value-of select="project"/></Cell>
            <Cell><xsl:value-of select="description"/></Cell>
        </Row>
    </xsl:template>

    <xsl:template match="day" name="day_t">
        <xsl:param name="name"/>
        <xsl:for-each select="entry">
            <xsl:call-template name="entry_t">
                <xsl:with-param name="name"><xsl:value-of select="$name"/></xsl:with-param>
                <xsl:with-param name="date"><xsl:value-of select="@date"/></xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="employee" name="employee_t">
        <xsl:for-each select="day">
            <xsl:call-template name="day_t">
                <xsl:with-param name="name"><xsl:value-of select="@name"/></xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="/weekreport">
                    <xsl:for-each select="employee">
                        <xsl:call-template name="employee_t"/>
                    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

由于某种原因,调用模板day_t完成,参数“name”设置为空值。为什么呢?..

4 个答案:

答案 0 :(得分:3)

当您传递参数时,当前节点不是employee元素,它是day元素(xsl:for- each更改当前节点)。因此,您尝试访问day元素的name属性,而不是其父employee元素。试试这个:

<xsl:call-template name="day_t">
    <xsl:with-param name="name"><xsl:value-of select="parent::employee/@name"/></xsl:with-param>
</xsl:call-template>

答案 1 :(得分:2)

您正在day_t内调用模板<xsl:for-each select="day">,其中包含@name的所有XPath表达式都是相对于元素<day>进行评估的。但是此元素没有属性name

答案 2 :(得分:1)

除了修复之外,还要考虑优化XSLT代码。

目前,您使用<xsl:for-each> 3次。您可以使用<xsl:apply-templates>

而不是这样做

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="entry" mode="entry_t">
        <xsl:param name="name"/>
        <xsl:param name="date"/>
        <Row>
            <Cell>
                <xsl:value-of select="$date"/>
            </Cell>
            <Cell>
                <xsl:value-of select="$name"/>
            </Cell>
            <Cell>
                <xsl:value-of select="time"/>
            </Cell>
            <Cell>
                <xsl:value-of select="project"/>
            </Cell>
            <Cell>
                <xsl:value-of select="description"/>
            </Cell>
        </Row>
    </xsl:template>
    <xsl:template match="day" mode="day_t">
        <xsl:param name="name1"/>
        <xsl:apply-templates mode="entry_t" select="entry">
            <xsl:with-param name="name" select="$name1"/>
            <xsl:with-param name="date" select="@date"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="employee" mode="employee_t">
        <xsl:apply-templates mode="day_t" select="day">
            <xsl:with-param name="name1" select="@name"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="/weekreport">
        <root>
            <xsl:apply-templates mode="employee_t" select="employee"/>
        </root>
    </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <Row>
        <Cell>25.06.2012</Cell>
        <Cell>Emp1</Cell>
        <Cell>08:00</Cell>
        <Cell>Proj1</Cell>
        <Cell>Bla-bla-bla</Cell>
    </Row>
    <Row>
        <Cell>25.06.2012</Cell>
        <Cell>Emp2</Cell>
        <Cell>08:00</Cell>
        <Cell>Proj2</Cell>
        <Cell/>
    </Row>
</root>

答案 3 :(得分:0)

您需要传递员工name和日date的值。

我做了一些修改,

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="entry" name="entry_t">
        <xsl:param name="name2"/>
        <xsl:param name="date1"/>
        <Row>
            <Cell>
                <xsl:value-of select="$date1"/>
            </Cell>
            <Cell>
                <xsl:value-of select="$name2"/>
            </Cell>
            <Cell>
                <xsl:value-of select="time"/>
            </Cell>
            <Cell>
                <xsl:value-of select="project"/>
            </Cell>
            <Cell>
                <xsl:value-of select="description"/>
            </Cell>
        </Row>
    </xsl:template>
    <xsl:template match="day" name="day_t">
        <xsl:param name="name1"/>
        <xsl:param name="date"/>
        <xsl:for-each select="entry">
            <xsl:call-template name="entry_t">
                <xsl:with-param name="name2" select="$name1"/>
                <xsl:with-param name="date1" select="$date"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="employee" name="employee_t">
        <xsl:param name="name"/>
        <xsl:for-each select="day">
            <xsl:call-template name="day_t">
                <xsl:with-param name="name1" select="$name"/>
                <xsl:with-param name="date" select="@date"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="/weekreport">
        <root>
            <xsl:for-each select="employee">
                <xsl:call-template name="employee_t">
                    <xsl:with-param name="name" select="@name"/>
                </xsl:call-template>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <Row>
        <Cell>25.06.2012</Cell>
        <Cell>Emp1</Cell>
        <Cell>08:00</Cell>
        <Cell>Proj1</Cell>
        <Cell>Bla-bla-bla</Cell>
    </Row>
    <Row>
        <Cell>25.06.2012</Cell>
        <Cell>Emp2</Cell>
        <Cell>08:00</Cell>
        <Cell>Proj2</Cell>
        <Cell/>
    </Row>
</root>