[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Hierarchy to Flat Structure


Subject: RE: [xsl] Hierarchy to Flat Structure
From: "Rick Quatro" <rick@xxxxxxxxxxxxxx>
Date: Sun, 17 Mar 2013 08:02:12 -0400

Hi Geert,

Thank you very much for all your efforts with this. The sibling recursion
works fine. I tried the XSLT 2 solution, but it doesn't work in my
environment. I am saving XML from Windows InDesign CS6 and I am not sure
what kind of processor it uses. But your solution below is excellent. Thanks
again for your help.

Rick

-----Original Message-----
From: Geert Bormans [mailto:geert@xxxxxxxxxxxxxxxxxxx] 
Sent: Sunday, March 17, 2013 6:11 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Hierarchy to Flat Structure

Hi Rick,

As I said, I would use sibling recursion.

Start at the first Category element and walk the following-sibling axis,
like this

<?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:template match="Cases/Cases">
         <Cases>
             <xsl:apply-templates select="Category[1]" />
         </Cases>
     </xsl:template>

     <xsl:template match="Category">
         <Case>
             <xsl:copy-of select="." />
             <xsl:apply-templates
select="following-sibling::*[1][not(self::Category)]"
mode="in-case-siblings"/>
         </Case>
         <xsl:apply-templates select="following-sibling::Category[1]"/>
     </xsl:template>

     <xsl:template match="*" mode="in-case-siblings">
         <xsl:copy-of select="." />
         <xsl:apply-templates
select="following-sibling::*[1][not(self::Category)]"
mode="in-case-siblings"/>
     </xsl:template>

     <xsl:template match="Category" mode="in-case-siblings"/>

  </xsl:stylesheet>

Cheers

Geert





At 02:57 17/03/2013, you wrote:
>I am trying to add hierarchy to a flat structure and have it basically 
>working. But I am wondering if there is a way to generalize the 
>children that I am adding. Hopefully an example will help illustrate what I
am doing.
>
>Input:
>
><Cases>
>         <Cases>
>                 <Category>Category One</Category>
>                 <CaseTitle>Category One Title</CaseTitle>
>                 <Institution>Category One Institution</Institution >
>                 <Author>Category One Author</Author>
>                 <History>Category One History</History>
>                 <Category>Category Two</Category>
>                 <CaseTitle>Category Two Title</CaseTitle>
>                 <Institution>Category Two Institution</Institution >
>                 <Author>Category Two Author</Author>
>                 <History>Category Two History</History>
>         </Cases>
></Cases>
>
>Output:
>
><Cases>
>         <Case>
>                 <Category>Category One</Category>
>                 <CaseTitle>Category One Title</CaseTitle>
>                 <Institution>Category One Institution</Institution >
>         </Case>
>         <Case>
>                 <Category>Category Two</Category>
>                 <CaseTitle>Category Two Title</CaseTitle>
>                 <Institution>Category Two Institution</Institution >
>         </Case>
></Cases>
>
>Stylesheet:
>
><?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:template match="Cases/Cases">
>     <Cases>
>         <xsl:apply-templates select="Category" />
>     </Cases>
>     </xsl:template>
>
>     <xsl:template match="Category">
>         <Case>
>             <Category><xsl:value-of select="." /></Category>
>             <xsl:apply-templates
>                 select="following-sibling::CaseTitle
>                 [generate-id(preceding-sibling::Category[1])
>                 = generate-id(current())]" />
>             <xsl:apply-templates
>                 select="following-sibling::Institution
>                 [generate-id(preceding-sibling::Category[1])
>                 = generate-id(current())]" />
>         </Case>
>     </xsl:template>
>
>     <xsl:template match="CaseTitle">
>         <xsl:copy-of select="." />
>     </xsl:template>
>
>     <xsl:template match="Institution">
>         <xsl:copy-of select="." />
>     </xsl:template>
>
></xsl:stylesheet>
>
>I have template rules for each of the following siblings of Category 
>and I can add more for Author, etc., but it seems like there should be 
>a way to generalize this for any number of elements. Any help would be
appreciated.
>
>Rick


Current Thread
Keywords