1

我正在使用 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

4 回答 4

3

当您传递参数时,当前节点不是员工元素,而是日元素(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>
于 2012-07-03T17:43:37.817 回答
2

您正在调用 template day_tinside <xsl:for-each select="day">,其中的所有 XPath 表达式,包括@name相对于 element 进行评估<day>。但是这个元素没有属性name

于 2012-07-03T17:40:58.443 回答
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>
于 2012-07-03T18:20:27.897 回答
0

您需要传递 employeename和 day的值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>
于 2012-07-03T17:54:50.360 回答