Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique value xsl

Status
Not open for further replies.

kimme123

Programmer
Oct 16, 2006
3
BE
Anyone can maybe help me with this problem?

Thanks in advance!

Kind regards, Kim


XSL FILE
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">

	<xsl:key name="key1" match="advise" use="concat(package/text(),class/text())"/>
	
	<xsl:template match="/">
		<html>
			<head>
				<title>Weave info</title>
			</head>
			<body>
				<xsl:apply-templates/>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="advise">
		<xsl:if test="count(key('key1',concat(package/text(),class/text()))[1]|self::node())=1" xml:space="preserve">
			<xsl:value-of select="concat(package/text(),'.',class/text())"/><br />
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>

XML FILE
Code:
<?xml version="1.0" ?>
<weaveinfo>
 <advise>
  <type>aaaa</type>
  <package>be.foo.igor</package>
  <class>Igortest</class>
 </advise>
 
 <advise>
  <type>bbbb</type>
  <package>be.foo.kim1</package>
  <class>TestKim</class>
 </advise>
 
 <advise>
  <type>cccc</type>
  <package>be.foo.kim2</package>
  <class>TestKim</class>
 </advise>
 
 <advise>
  <type>dddd</type>
  <package>be.foo.kim2</package>
  <class>TestKim</class>
 </advise>
 
 <advise>
  <type>eeee</type>
  <package>be.foo.kim2</package>
  <class>TestKim</class>
 </advise>
 
  <advise>
  <type>eeee</type>
  <package>be.foo.kim2</package>
  <class>TestKim</class>
 </advise>
 
 <advise>
  <type>eeee</type>
  <package>be.foo.kim2</package>
  <class>TestKim</class>
 </advise>
</weaveinfo>

RESULT
be.foo.igor.Igortest
be.foo.kim1.TestKim
be.foo.kim2.TestKim

EXPECTED RESULT
be.foo.igor.Igortest
aaaa

be.foo.kim1.TestKim
bbbb

be.foo.kim2.TestKim
cccc
dddd
eeee
 
Kim,

What you are doing is grouping, based upon the value of [tt]concat(package/text(),class/text())[/tt]. This has been described in the following recent threads:[ul][li]thread426-1210886[/li][li]thread426-1328351[/li][li]thread426-1332802[/li][/ul]

Your effort is a valiant attempt, but grouping in XSLT v1 is a bit difficult. In particular, you are failing to iterate over all the nodes that have identical key values. The referenced threads all demonstrate the Muench method for grouping.

Give it a try and let us know if you get the expected results.

Tom Morrison
 
I have tried, though I have no idea what I'm doing, and I came up with this.

The only difference is there are no <br /> after each group, but it's close.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">

    <xsl:key name="key1" match="advise" use="concat(package/text(),class/text())"/>
    <xsl:key name="key2" match="advise" use="type/text()"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Weave info</title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="advise">
        <xsl:if test="count(key('key1',concat(package/text(),class/text()))[1]|self::node())=1" xml:space="preserve">
            <xsl:value-of select="concat(package/text(),'.',class/text())"/><br />
        </xsl:if>
	<xsl:if test="count(key('key2',type/text())[1]|self::node())=1" xml:space="preserve">	
		<xsl:value-of select="type/text()"/><br />
	</xsl:if>
    </xsl:template>
</xsl:stylesheet>

[small]"King Coleman, live foreva!! Yeah buddy, light weight!!!"[/small]
<.
 
Try this
Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
    <xsl:key name="key1" match="//advise" use="concat(package/text(),class/text())"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>Weave info</title>
            </head>
            <body>
    <xsl:for-each select="//advise[generate-id(.)=generate-id(key('key1', concat(package/text(),class/text()))[1])]">
            <xsl:value-of select="concat(package/text(),'.',class/text())"/><br />
		<xsl:for-each select="key('key1', concat(package/text(),class/text()))">
		<xsl:sort select="type"/>
		    <xsl:variable name="lastType" select="type"/>
            <xsl:if test="not(preceding-sibling::advise[type=$lastType])">
			    <xsl:value-of select="type"/><br />
			</xsl:if>
		</xsl:for-each>
	</xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Tom Morrison
 
Hi k5tm thank you for your help. I'm testing it now and it works well. Sorry for the late reply but I was abroad last week. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top