[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Siblings to child - Hierarchy

Subject: Re: Siblings to child - Hierarchy
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Fri, 22 Aug 2003 19:25:21 +0200
child hierarchy xml
Use the following transformation:

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

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="rowset">
    <site>
      <xsl:apply-templates select="row[site_parent = 0]"/>
    </site>
  </xsl:template>

  <xsl:template match="row">
    <xsl:copy-of select="site_id | site_name"/>
    <xsl:variable name="vChildren"
        select="../row[site_parent = current()/site_id]"/>
    <xsl:if test="$vChildren">
      <site>
        <xsl:apply-templates select="$vChildren"/>
      </site>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

When applied against your source.xml:

<page>
  <rowset>
    <row>
      <site_id>1</site_id>
      <site_name>Test Parent1</site_name>
      <site_parent>0</site_parent>
      <level>1</level>
      <path>/0</path>
    </row>
    <row>
      <site_id>2</site_id>
      <site_name>Test Child1 of Test Parent1</site_name>
      <site_parent>1</site_parent>
      <level>2</level>
      <path>/0/1</path>
    </row>
    <row>
      <site_id>4</site_id>
      <site_name>Test Child1 of Test Child1</site_name>
      <site_parent>2</site_parent>
      <level>3</level>
      <path>/0/1/2</path>
    </row>
    <row>
      <site_id>3</site_id>
      <site_name>Test Parent2</site_name>
      <site_parent>0</site_parent>
      <level>1</level>
      <path>/0</path>
    </row>
  </rowset>
</page>

the wanted result is produced:

<site>
  <site_id>1</site_id>
  <site_name>Test Parent1</site_name>
  <site>
    <site_id>2</site_id>
    <site_name>Test Child1 of Test Parent1</site_name>
    <site>
      <site_id>4</site_id>
      <site_name>Test Child1 of Test Child1</site_name>
    </site>
  </site>
  <site_id>3</site_id>
  <site_name>Test Parent2</site_name>
</site>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"Todd Kleine" <tkleine@xxxxxxxxxxxxx> wrote in message
news:000601c368be$ebce32e0$6401a8c0@xxxxxxxxxxxx
> Hello,
>
> I am new to xsl and am having a tough time with a template to transform
the
> results of an oracle "connect by" query into a hierarchical xml document.
>
> The xml results of a sample query is below:
>
> <page>
>   <rowset>
>     <row>
>       <site_id>1</site_id>
>       <site_name>Test Parent1</site_name>
>       <site_parent>0</site_parent>
>       <level>1</level>
>       <path>/0</path>
>     </row>
>     <row>
>       <site_id>2</site_id>
>       <site_name>Test Child1 of Test Parent1</site_name>
>       <site_parent>1</site_parent>
>       <level>2</level>
>       <path>/0/1</path>
>     </row>
>     <row>
>       <site_id>4</site_id>
>       <site_name>Test Child1 of Test Child1</site_name>
>       <site_parent>2</site_parent>
>       <level>3</level>
>       <path>/0/1/2</path>
>     </row>
>     <row>
>       <site_id>3</site_id>
>       <site_name>Test Parent2</site_name>
>       <site_parent>0</site_parent>
>       <level>1</level>
>       <path>/0</path>
>     </row>
>   </rowset>
> </page>
>
> where the site_parent value is the site_id of it's parent and a
site_parent
> of 0 is a top-level node.
>
> I would like to produce a hierarchical xml doc based upon these results of
> the form
>
> <site>
>   <site_id>1</site_id>
>   <site_name>Test Parent1</site_name>
>   <site>
>     <site_id>2</site_id>
>     <site_name> Test Child1 of Test Parent1</site_name>
>     <site>
>       <site_id>4</site_id>
>       <site_name>Test Child2 of Test Child1</site_name>
>     </site>
>   </site>
> </site>
> <site>
>   <site_id>3</site>
>   <site_name>Test Parent2</site_name>
> </site>
>
> Has anyone had experience with this type of transform?  Any help would be
> appreciated.
>
> Thanks in advance
>
> todd
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.