1
votes

I'm writing simple transformation using XSLT 1.0 and getting strange result.. Looks like I don't understand something, but I found I can't select attribute of a node for some reason. Here is my input 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>

and here is 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>

For some reason calling template day_t is done with parameter "name" set to empty value. Why?..

4

4 Answers

3
votes

When you pass the param, the current node isn't an employee element, it's a day element (xsl:for-each changes the current node). Therefore, you're attempting to access the name attribute on the day element, not its parent employee element. Try this instead:

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

You are calling template day_t inside <xsl:for-each select="day">, and all the XPath expressions inside it, including @name are evaluated relative to element <day>. But this element doesn't have attribute name.

1
votes

Apart from fix, think of optimization of XSLT code too.

Currently, you are using <xsl:for-each> for 3 times. Rather than doing this way, you can do using <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>

OUTPUT:

<?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>
0
votes

You need to pass the value for employee name and day date.

I have done some modification,

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>

OUTPUT:

<?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>