xsl-list
[Top] [All Lists]

Re: This could be simple, but not for me!

2004-05-25 08:56:23
Hi Kenny,
  You are first grouping by <Detail>, and then you
want to do a 2nd level grouping by <City>. IMO, this
seems not very logical! (if I am not understanding
something wrong).

Please try this XSL (this does not provide solution to
your second requirement) -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="text" version="1.0"
encoding="UTF-8" indent="yes"/>
        
<xsl:key name="detail-key" match="Detail" use="." />
        
<xsl:template match="/Root">
    <xsl:for-each select="//Detail">
      <xsl:if test="generate-id(.) =
generate-id(key('detail-key', .)[1])">
        <xsl:value-of select="@no" /><xsl:text>,
</xsl:text><xsl:value-of select="."
/><xsl:text>&#xa;</xsl:text>
        <xsl:for-each select="key('detail-key', .)">
          <xsl:value-of select="../../../../City"
/><xsl:text>&#xa;</xsl:text>
          <xsl:value-of select="../../Name"
/><xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
     </xsl:if>
   </xsl:for-each>
</xsl:template>
        
</xsl:stylesheet>

Regards,
Mukul

--- "Kenny Bogoe (BogoeMD)" <kenny(_at_)bogoe(_dot_)com> wrote:
Thanks, Andrew

This is very very close to what I need. But the city
name has to be there
only once for all names like:


100, Red
    City1
        Name1
        Name10
        Name100
        ...etc

    City2
        Name4
        Name40
        ...

200, Blue
    City1
        Name2
        Name20
        ...

    City2
        Name5
        ...

300, Green
    City1
        Name3
        Name30
        Name300
        Name3000
        ...

400, Yellow
    City2
        Name6
        ...

Is that possible as well?

/Kenny



Hi,

Though I have been working sometime with XSLT, I
am having
trouble with this transformation. Anyone know how
to do this?
The XML source is from a database and is really
really huge
in size, so performance of the transformation is
very critical...


<Root>
    <Community>
        <City>City1</City>
        <A>
            <B>
                <Name>Name1</Name>
                <Info>
                    <Detail no="100">Red</Detail>
                </Info>
            </B>
            <B>
                <Name>Name2</Name>
                <Info>
                    <Detail
no="200">Blue</Detail>
                </Info>
            </B>
            <B>
                <Name>Name3</Name>
                <Info>
                    <Detail
no="300">Green</Detail>
                </Info>
            </B>
        </A>
    </Community>
    <Community>
        <City>City2</City>
        <A>
            <B>
                <Name>Name4</Name>
                <Info>
                    <Detail no="100">Red</Detail>
                </Info>
            </B>
            <B>
                <Name>Name5</Name>
                <Info>
                    <Detail
no="200">Blue</Detail>
                </Info>
            </B>
            <B>
                <Name>Name6</Name>
                <Info>
                    <Detail
no="400">Yellow</Detail>
                </Info>
            </B>
        </A>
    </Community>
</Root>


This is the result I need to produce:


100, Red

    City1
        Name1
    
    City2
        Name4
    
    
200, Blue

    City1
        Name2
        
    City2
        Name5
        
300, Green

    City1
        Name3
        
400, Yellow

    City2
        Name6

Heres a stylesheet that will do it.  It's a
grouping problem, you just
need to group <Detail> elements by their id
attibute and text content,
then apply templates to only the first one in the
group.  Check out
jeni's site for more on grouping
http://www.jenitennison.com

(I've html like tags in the output rather than
whitespace so my IDE can
tidy it for me :) You may want to replace the
ancestor:: use with
another key to help performance)

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:key name="details" match="Detail"
use="concat(@no,'/',.)"/>

<xsl:template match="/">
<root>
  <xsl:apply-templates
select="//Detail[generate-id() =

generate-id(key('details',concat(@no,'/',.))[1])]"/>
</root>
</xsl:template>

<xsl:template match="Detail">
<div detail="{concat(@no,', ',.)}">
  <xsl:for-each
select="key('details',concat(@no,'/',.))">
    <span>
      <xsl:value-of
select="ancestor::Community/City"/>,<xsl:text/>
      <xsl:value-of
select="parent::Info/preceding-sibling::Name"/>
    </span>
</xsl:for-each>
</div>
</xsl:template>

</xsl:stylesheet>


cheers
andrew



        
                
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/