Hi, My issue with xml is, I have an xml like

<a><b><c><d1 value="x" weight="10"/>
<d2 value="y" weight="20"/>
<d3 value="z" weight="30"/>
</c></b></a>

I need to conver this xml to,

<a><b>
<c><d1 value="x" weight="10"/>
<d2 value="y" weight="20"/>
</c>
<d3 value="z" weight="30"/>
</b>
</a>

I need move specific "d3" element (with all attributes) to another level level of xml.
i.e. move "d3" element from <c> element to <b> element using xsl

Thanks,
Desu.

Recommended Answers

All 3 Replies

I try to make a generic code, i expect it's not too difficult to endurstand

<?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" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:key name="copy" match="c/d1|c/d2" use="generate-id(..)"/>
	<xsl:key name="up" match="c/d2" use="generate-id(..)"/>
	<xsl:template match="node()">
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="text()">
		<xsl:value-of select="."/>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
	</xsl:template>
	<xsl:template match="c">
		<xsl:copy>
			<xsl:apply-templates select="key('copy',generate-id(.))"/>
		</xsl:copy>
		<xsl:apply-templates select="key('up',generate-id(.))"/>
	</xsl:template>
</xsl:stylesheet>

in key up nodes you want move
in key copy nodes you want unmove
and you need a template on parent element od these nodes

Thank you Erwan. It works fine.

Done

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.