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 October 18th, 2005, 09:03 PM
wu4 wu4 is offline
Registered User
 
Join Date: Oct 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default flat to hierarchy

I am convert a flat class file to a class hierarchy file. ie, every class only shows once.
If a class is subclass of other class, it will be show as subclass of that class and not show
as the direct child of classes.

I am new with xslt, please give me help.

The source file is like this
Code:
<classes>
    <class name="A"/>
    <class name="B"/>
    <class name="C">
       <class name="D"/>
    </class>
    <class name="D">
       <class name="E"/>
       <class name="F"/>
    </class>
    <class name="E">
       <class name="G"/>
    </class>
    <class name="F"/>
    <class name="G"/>
</classes>
The output file corresponding above file should be
Code:
<classes>
    <class name="A"/>
    <class name="B"/>
    <class name="C">
       <class name="D">
          <class name="E">
             <class name="G"/>
          </class>
          <class name="F"/>
       </class>
    </class>
</classes>
 
Old October 19th, 2005, 02:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Do a recursive tree walk using apply-templates as you would to traverse a normal XML tree, but instead of following the physical hierarchy (i.e. processing the children of a node using the child axis), follow the logical hierarchy. It's useful to set up a key

<xsl:key name="n" match="class" use="@name"/>

and then you can process a node using

<xsl:template match="class">
  ...
  <xsl:apply-templates select="key('n', @name)/child::node()"/>
  ...
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 19th, 2005, 12:52 PM
wu4 wu4 is offline
Registered User
 
Join Date: Oct 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you please give more details.

When we use <xsl:apply-templates select="key('n', @name)/child::node()"/>, how we can prevent a node is processed more than once. Just like above example, we have to node with name attribute D. When we process /classes/class[@name='D'], we call <xsl:apply-templates select="key('n', @name)/child::node()"/>, then we process /classes/class/class[@name='D'], we call <xsl:apply-templates select="key('n', @name)/child::node()"/>,then we process /classes/class[@name='D'], ...., it seems that will enter a unlimited loop.

Don't langh at me if I am wrong. I am new with xslt, it drives me crazy.
Do you think it's convenient that I send you email directly? If yes, please send me a email.




Thanks
 
Old October 19th, 2005, 03:36 PM
wu4 wu4 is offline
Registered User
 
Join Date: Oct 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is my code. I created it by using
Quote:
quote:
"Michael Kay The trick to this is to write a stylesheet with template rules that use <xsl:apply-templates> to process the children of the current element in the usual way, but instead of selecting the physical children of an element, you select its logical children, based on following the relationships in your data. The only other thing needed is to start processing at the logical root of the tree"
http://www.dpawson.co.uk/xsl/sect2/f....html#d4658e13

But the results I got was wrong. Could someone explain to me, why?

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">


  <xsl:key name="n" match="class" use="@name"/>

  <xsl:template match="/">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="classes">
    <classes>

      <xsl:apply-templates select="class[count(key('n', @name))=1]"/>
    </classes>
  </xsl:template>

  <xsl:template match="class">
    <xsl:choose>
      <xsl:when test="class">
        <class>
          <xsl:apply-templates select="@* | Parameters"/>
          <xsl:for-each select="class">

             <xsl:variable name="myname">
               <xsl:value-of select="@name"/>
             </xsl:variable>

             <xsl:apply-templates select="/classes/class[@name=$myname]"/>
          </xsl:for-each>
        </class>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template match="@*">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="Parameter">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
TREE / HIERARCHY pallone XSLT 3 June 13th, 2008 07:45 AM
Flat DB structure into XML Hierarchy DaveQuested XSLT 1 January 29th, 2007 10:25 AM
Hierarchy Navigation asearle XSLT 0 September 28th, 2006 02:27 AM
Namespace Hierarchy projectedNexus BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 6th, 2003 09:56 PM





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