Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2004, 09:16 AM
Registered User
 
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grouping XML by Muenchian Method - Urgent :-(

Hello,


I am trying to solve following problem:

Today I use identity transform. to filter elements from my original XML file and create XML output file.

I have created two XSLTs (base.xslt and filter.xslt), where base.xslt does the identity transformation
and filter.xslt defines filtering rules and templates.

My input XML looks like:

[Input XML]

Code:
<Documents>
    <Document chapter="1" title="title 1" href="file1.xml">
          <Article title="1.1" info="sub"/>
          <Article title="1.2" info="main"/>          
     </Document>
    <Document chapter="2" title="title 2" href="file2.xml">
          <Article title="2.1" info="sub"/>
          <Article title="2.2" info="main"/>          
     </Document>
</Documents>
What I basically want is to filter Article elements where info="sub", create parent node "sub" and group them under
parent node.

Same thing with with Article elements where info="main", create parent node "main" and group them under
parent node.

One parent node for each category. (see in Filtered XML - how I want output to look like).

[Output XML]

Code:
<Documents>
    <Document name="main">
          <Article title="1.2" info="main"/>
          <Article title="2.2" info="main"/>
     </Document>
    <Document name="sub">
          <Article title="1.1" info="sub"/>         
          <Article title="2.1" info="sub"/>          
     </Document>
</Documents>
[base.xslt]

Code:
<?xml version="1.0" encoding="UTF-8"?>

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

     
        <xsl:include href="filter.xslt"/>

     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
     <xsl:template match="node() | @*">
          <xsl:copy>
               <xsl:apply-templates select="node() | @*"/>
          </xsl:copy>
     </xsl:template>
</xsl:stylesheet>
[filter.xslt]

Code:
<?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:template match="Doc[not(@info='main')][not(@info='sub')]"/>
</xsl:stylesheet>

If this can't be done the way I suggested, could you give me an example on how it can be done?

Thank you in advance!


.Michael
 
Old September 27th, 2004, 09:54 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think this is more of a grouping problem:

http://www.jenitennison.com/xslt/gro...muenchian.html



--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 27th, 2004, 12:32 PM
Registered User
 
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I have tried to implement the grouping by using the Muenchian Method according to http://www.jenitennison.com/xslt/gro...muenchian.html, but I still receive errors when I transform.

I am trying to start with Jeni's example, but keep my XML structure. (below)

"Let's take our address book above. We want to group the contacts according to their surname, so we create a key that assigns each contact a key value that is the surname given in the record. The nodes that we want to group should be matched by the pattern in the 'match' attribute. The key value that we want to use is the one that's given by the 'use' attribute:"

xml-file structure

<?xml version="1.0" encoding="utf-8"?>

<Records>
    <Contact id="0001">
        <Doc title="Mr"/>
        <Doc forename="John"/>
        <Doc surname="Smith"/>
    </Contact>
    <Contact id="0002">
        <Doc title="Dr"/>
        <Doc forename="Amy"/>
        <Doc surname="Jones"/>
    </Contact>
</Records>



xslt-file

<?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="contacts-by-surname" match="Doc" use="surname" />

    <xsl:template match="Records">
        <xsl:for-each select="Doc[count(. | key('contacts-by-surname', surname)[1]) = 1]">
            <xsl:sort select="surname" />
                <xsl:value-of select="surname" />,<br />
                    <xsl:for-each select="key('contacts-by-surname', surname)">
                        <xsl:sort select="forename" />
                        <xsl:value-of select="forename" /> (<xsl:value-of select="title" />)<br />
                    </xsl:for-each>
        </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>


What is the problem? Can anybody see it ?

Thanx,

/M
 
Old September 28th, 2004, 03:21 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

So what does your version of Jeni's grouping stylesheet look like? You have to define your own key etc.



--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 28th, 2004, 03:50 AM
Registered User
 
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I need to modify xslt stylesheet file "XSLT stylesheet" so it can give me an XML output-file that looks similar to "Wanted XML Output":

Input XML

Code:
<records>
    <contact id="0001">
        <doc title="Mr"/>
        <doc forename="John"/>
        <doc surname="Smith"/>
    </contact>
    <contact id="0002">
        <doc title="Dr"/>
        <doc forename="Amy"/>
        <doc surname="Jones"/>
    </contact>
</records>

Wanted XML Output

Code:
<records>
    <contact id="0001" surname="Jones">
        <doc forename="Amy" title="Dr"/>    
        <doc forename="Brian" title="Mr"/>
    </contact>
    <contact id="0002" surname="Smith">
        <doc forename="Fiona" title="Ms"/>    
        <doc forename="John" title="Mr"/>
    </contact>
</records>

XSLT stylesheet

Code:
<?xml version="1.0" encoding="UTF-8"?>

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

     
    <xsl:key name="contacts-by-surname" match="contact" use="surname" />
     
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
     
    <xsl:template match="records">
        <xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]">
            <xsl:sort select="surname" />
                <xsl:value-of select="surname" />,<br />
                    <xsl:for-each select="key('contacts-by-surname', surname)">
                        <xsl:sort select="forename" />
                        <xsl:value-of select="forename" /> (<xsl:value-of select="title" />)<br />
                    </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

     
</xsl:stylesheet>
 
Old September 28th, 2004, 04:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your code appears to use "surname" where it should use "doc/@surname".

Michael Kay
http://www.saxonica.com/





Similar Threads
Thread Thread Starter Forum Replies Last Post
help with Muenchian method grouping and html table mickhughes XSLT 3 May 1st, 2008 06:24 AM
help with Muenchian method grouping... agentdz015 XSLT 1 April 7th, 2008 04:53 PM
Muenchian grouping amhicraig XSLT 1 December 5th, 2007 06:43 PM
Muenchian Grouping Chamkaur XSLT 1 June 21st, 2006 10:51 AM
Advanced Grouping Using the Muenchian Method Bodiam XSLT 0 August 8th, 2005 11:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.