[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: [xsl 1.0] howto merge branches by name

Subject: Re: [xsl 1.0] howto merge branches by name
From: Adrian Herscu <adrian.herscu@xxxxxxxxx>
Date: Wed, 14 Dec 2011 22:37:10 +0200
 Re: [xsl 1.0] howto merge branches by name
Thanks for the fast reply, Ken :)

I am using XSL 1.0.

My last attempt to solve it this evening was:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:key name="suites" match="//suite" use="@name" />
	<xsl:key name="cases" match="//case" use="concat(../@name, '.', @name)" />

	<xsl:template match="/">
		<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="test">
		<test>
			<xsl:for-each select="//suite">
				<xsl:if test="generate-id() = generate-id(key('suites',@name)[1])">
					<xsl:apply-templates select="." />
				</xsl:if>
			</xsl:for-each>
		</test>
	</xsl:template>

<xsl:template match="suite">
<suite name="{@name}">
<xsl:for-each select="//case">
<xsl:if test="generate-id() = generate-id(key('cases',concat(../@name, '.', @name))[1])">
<xsl:apply-templates select="." />
</xsl:if>
</xsl:for-each>
</suite>
</xsl:template>


	<xsl:template match="case">
		<case name="{@name}" />
	</xsl:template>
</xsl:stylesheet>

which rendered duplicate cases :(

<?xml version="1.0" encoding="UTF-8"?>
<test>
	<suite name="A">
		<case name="A" />
		<case name="B" />
		<case name="B" />
	</suite>
	<suite name="C">
		<case name="A" />
		<case name="B" />
		<case name="B" />
	</suite>
</test>

Adrian.

On 12/14/2011 10:27 PM, G. Ken Holman wrote:
At 2011-12-14 22:20 +0200, Adrian Herscu wrote:
Consider the following input XML file:
...
I am trying to find a way to transform it into:
...
The branches are always 3-levels deep. That is: suite > case > procedure.

Please help,

You don't say what you want help with, or where you are having problems. Nor do you say what limitations you have on which version of XSLT to use. That makes it difficult to advise you on how to help with your understanding.

Below is an XSLT 2 solution that I hope helps.

. . . . . . . . . . Ken

t:\ftemp>type adrian.xml
<?xml version="1.0" encoding="UTF-8"?>
<test>
<suite name="A">
<case name="A">
<procedure name="A" />
</case>
</suite>
<suite name="A">
<case name="A">
<procedure name="B" />
</case>
</suite>
<suite name="A">
<case name="B">
<procedure name="A" />
</case>
</suite>
</test>

t:\ftemp>xslt2 adrian.xml adrian.xsl
<?xml version="1.0" encoding="UTF-8"?>
<test>
<suite name="A">
<case name="A">
<procedure name="A"/>
<procedure name="B"/>
</case>
<case name="B">
<procedure name="A"/>
</case>
</suite>
</test>
t:\ftemp>type adrian.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="test">
<test>
<xsl:for-each-group select="suite" group-by="@name">
<suite name="{@name}">
<xsl:for-each-group select="current-group()/case" group-by="@name">
<case name="{@name}">
<xsl:copy-of select="current-group()/procedure"/>
</case>
</xsl:for-each-group>
</suite>
</xsl:for-each-group>
</test>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>



--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/uoui9h
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.