简体   繁体   English

XSLT:无法选择属性

[英]XSLT: Can't select attribute

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. 我正在使用XSLT 1.0编写简单的转换,并得到奇怪的结果。看起来好像我不明白,但由于某种原因我发现无法选择节点的属性。 Here is my input XML: 这是我的输入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: 这是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. 由于某种原因,调用模板day_t时会将参数“名称”设置为空值。 Why?.. 为什么?..

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). 传递参数时,当前节点不是employee元素,而是day元素(xsl:for-each会更改当前节点)。 Therefore, you're attempting to access the name attribute on the day element, not its parent employee element. 因此,您尝试访问day元素上的name属性,而不是其父雇员元素。 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>

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> . 您正在<xsl:for-each select="day">中调用模板day_t ,并且其中的所有XPath表达式(包括@name都相对于元素<day>进行求值。 But this element doesn't have attribute name . 但是这个元素没有属性name

Apart from fix, think of optimization of XSLT code too. 除了修复之外,还应该考虑XSLT代码的优化。

Currently, you are using <xsl:for-each> for 3 times. 当前,您正在使用<xsl:for-each> 3次。 Rather than doing this way, you can do using <xsl:apply-templates> 您可以使用<xsl:apply-templates><xsl:apply-templates>这种方式

XSLT: 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>

You need to pass the value for employee name and day date . 您需要传递员工namedate的值。

I have done some modification, 我做了一些修改,

XSLT: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM