sanjay
sanjay

Reputation: 1020

XSLT - templates not applied to some nodes

I have a xml like this,

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
        <country>USA</country>
        <company>EMI</company>
        <price>8.20</price>
        <year>1987</year>
    </cd>
</catalog>

What I need to do is remove <year> node from original xml, change <artist> node as <name> and add new nodes(<time>, <version>) at the end of final <cd> node.

I've written following XSLT code,

<xsl:variable name="time" as="xs:dateTime" select="current-dateTime()"/>
<xsl:variable name="version" as="xs:double" select="1.0"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="catalog/cd/artist">
        <name><xsl:apply-templates/></name>
    </xsl:template>

    <xsl:template match="catalog/cd/year"/>

    <xsl:template match="catalog/cd[last()]">
        <xsl:copy-of select="."/>
        <time><xsl:value-of select="$time"/></time>
        <version><xsl:value-of select="$version"/></version>
    </xsl:template>

Output is follows,

<?xml version="1.0" encoding="UTF-8"?><catalog>
    <cd>
        <title>Empire Burlesque</title>
        <name>Bob Dylan</name>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>

    </cd>
    <cd>
        <title>Hide your heart</title>
        <name>Bonnie Tyler</name>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>

    </cd>
    <cd>
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
        <country>USA</country>
        <company>EMI</company>
        <price>8.20</price>
        <year>1987</year>
    </cd>
    <time>2015-06-29T13:41:49.885+05:30</time>
    <version>1</version>
</catalog>

As you see, code is working fine but only final node seems not applied the templates (in final node <artist> node has not changed <year> node appears).

How can I solve this problem.

Upvotes: 0

Views: 69

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167581

Change

<xsl:template match="catalog/cd[last()]">
    <xsl:copy-of select="."/>
    <time><xsl:value-of select="$time"/></time>
    <version><xsl:value-of select="$version"/></version>
</xsl:template>

to

<xsl:template match="catalog/cd[last()]">
    <xsl:copy>
      <xsl:apply-templates>
      <time><xsl:value-of select="$time"/></time>
      <version><xsl:value-of select="$version"/></version>
    </xsl:copy>
</xsl:template>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117018

In your last template, replace:

<xsl:copy-of select="."/>

with:

<xsl:copy>
    <xsl:apply-templates/>
</xsl:copy>

Upvotes: 1

Related Questions