1

Imagine I have an xml like this:

<years>
    <year yearValue="2011">
        <month monthValue="1" workTime="10">
        <month monthValue="2" workTime="90">
    </year>
    <year yearValue="2012">
        <month monthValue="2" workTime="90">
    </year>
</years>

So when I am looping in my XML file

<xsl:for-each select="activityExport/years">
    <xsl:for-each select="year">
        <xsl:for-each select="month">
            <xsl:value-of select="@workTime"/>
            <xsl:value-of select="@monthValue"/>

However I also want to show the yearValue just after @workTime.

How can I do this?

Thanks.

1 Answer 1

2

The value you specify in select attribute of the xsl:value-of tag is a XPath expression. Base on XPath syntax, this value is the address of some tag or attribute in source document that has to be written on output. You can use relative paths in XPath expressions of your innermost loop to access yearValue attribute of the parent tag; something like '../@yearValue'.

So I think something like the this could be your answer:

<xsl:for-each select="activityExport/years">
    <xsl:for-each select="year">
        <xsl:for-each select="month">
            <xsl:value-of select="@workTime"/>
            <xsl:value-of select="../@yearValue"/>
            <xsl:value-of select="@monthValue"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.