This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Reformatting a flat XML doc into an XML hierarchy based on 2 keys
- To: "'XSL-List at lists dot mulberrytech dot com'" <XSL-List at lists dot mulberrytech dot com>
- Subject: [xsl] Reformatting a flat XML doc into an XML hierarchy based on 2 keys
- From: "Krishnaprasad,Vidhya" <Vidhya dot Krishnaprasad at onehealthbank dot com>
- Date: Tue, 3 Apr 2001 09:56:29 -0400
- Reply-To: xsl-list at lists dot mulberrytech dot com
> Hi,
>
> I want to apply XSL to the following XML file with the desired output
> listed below:
>
> ******************** INPUT XML FILE *******************************
> <?xml version="1.0" encoding="UTF-8"?>
> <data>
> <row>
> <year>2001</year>
> <acct>1</acct>
> <acctname>A</acctname>
> </row>
> <row>
> <year>2002</year>
> <acct>2</acct>
> <acctname>B</acctname>
> </row>
> <row>
> <year>2002</year>
> <acct>2</acct>
> <acctname>C</acctname>
> </row>
> <row>
> <year>2004</year>
> <acct>1</acct>
> <acctname>D</acctname>
> </row>
> <row>
> <year>2004</year>
> <acct>2</acct>
> <acctname>E</acctname>
> </row>
> </data>
>
> ********************* DESIRED OUTPUT XML FILE ************************
> <?xml version="1.0" encoding="UTF-8"?>
> <data>
> <ActivityDate year="2001">
> <Acct id="1"><Name>A</Name></Acct>
> </ActivityDate>
> <ActivityDate year="2002">
> <Acct id="2">
> <Name>B</Name>
> <Name>C</Name>
> </Acct>
> </ActivityDate>
> <ActivityDate year="2002">
> <Acct id="1">
> <Name>D</Name>
> </Acct>
> <Acct id="2">
> <Name>E</Name>
> </Acct>
> </ActivityDate>
> </data>
>
> ************************* XSL file I'm trying to use with no effect
> ***********************
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output indent="yes"/>
> <xsl:key name="foo" match="data/row" use="year"/>
> <xsl:key name="bar" match="data/row" use="acct"/>
> <xsl:template match="/">
> <data>
> <xsl:for-each select="data/row[generate-id(.) =
> generate-id(key('foo',year))]">
> <xsl:element name="ActivityDate">
> <xsl:attribute name="year"><xsl:value-of
> select="year"/></xsl:attribute>
> <xsl:for-each select="key('foo', year)">
> <xsl:for-each
> select="data/row[concat(generate-id(key('foo',
> year)),'::',generate-id(key('bar',acct)))=
>
> concat(generate-id(.),'::',generate-id(key('bar',acct)))]">
> <xsl:element name="Acct">
> <xsl:attribute
> name="id"><xsl:value-of select="acct"/></xsl:attribute>
> <xsl:for-each
> select="key('bar', acct)">
> <Name>
> <xsl:value-of
> select="acctname"/>
> </Name>
> </xsl:for-each>
> </xsl:element>
> </xsl:for-each>
> </xsl:for-each>
> </xsl:element>
> </xsl:for-each>
> </data>
> </xsl:template>
> </xsl:stylesheet>
>
>
> ***** the output when I run this xsl is *************
> <?xml version="1.0" encoding="UTF-16"?>
> <data>
> <ActivityDate year="2001">
> </ActivityDate>
> <ActivityDate year="2002">
> </ActivityDate>
> <ActivityDate year="2004">
> </ActivityDate>
> </data>
>
>
> What am I doing incorrectly. I appreciate any help in this regard.
>
> Thanks
> Vidhya
>
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list