tags:

views:

20

answers:

1

how to use xsl to change name of child node if it = to the parent node and apply to all nodes

example

< items >
< items >3</items >
</items >

to

< items >
< parentname-"inner"childname >3</parentname-"inner"childname >
</items >

thank you very much

+2  A: 

If I understand your question correctly, and you want to generate the following XML from your sample XML:

<?xml version="1.0" encoding="UTF-16"?>
<items>
<items-items>3</items-items>
</items>

then the following XSLT can be used:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output indent="yes" />

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

<!--Match elements who's name is equal to it's parent -->
<xsl:template match="*[name()=name(..)]">
    <!--create an element using the name of the parent element, followed by a "-", followed by the matched element name -->
    <xsl:element name="{name(..)}-{name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>
Mads Hansen