xsl-list
[Top] [All Lists]

Re: [xsl] Creating Hierarchy

2008-10-17 09:19:39
At 2008-10-17 13:47 +0100, Rowan Sylvester-Bradley wrote:
You'll find a description of an XSLT 2.0 stylesheet that does this (and some
other things besides) at

http://www.idealliance.org/proceedings/xml04/papers/111/mhk-paper.html

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

Thanks to Michael and Ken for your replies. I've now got it more or less working using xsl:for-each-group. However, I've got another problem that I can't quite work out. Each of the main elements in the result file needs to have a unique ID attribute.

An ID attribute value must be a name token, so your "1","2",... won't be correct. But you could use something like "N1","N2",...

So if my source file is:

<mytree>
  <node>
    <name>Root of my tree</name>
    <level>0</level>
  </node>
  <node>
    <name>Child of root</name>
    <level>1</level>
  </node>
  <node>
    <name>Another child of root</name>
    <level>1</level>
  </node>
  <node>
    <name>Grandchild of root</name>
    <level>2</level>
  </node>
  <node>
    <name>Yet another child of root</name>
    <level>1</level>
  </node>
</mytree>

I want to transform this into this:

<newnode id="1">
  <name>Root of my tree</name>
  <newnode id="2">
    <name>Child of root</name>
  </newnode>
  <newnode id="3">
    <name>Another child of root</name>
    <newnode id="4">
      <name>Grandchild of root</name>
    </newnode>
  </newnode>
  <newnode id="5">
    <name>Yet another child of root</name>
  </newnode>
</newnode>

I.e. the id attributes just keep incrementing regardless of the hierarchy.

How do I generate the values of these id attributes?

Assuming the current node is <node>, then <xsl:number/> can be used without attributes to count the current node amongst its siblings in the source tree:

  <xsl:attribute name="id">N<xsl:number/></xsl:attribute>

The "video sample lesson" link in my trailer below is the YouTube copy of my lesson on <xsl:number/> if you need to make changes.

I hope this helps.

. . . . . . . . . . . . Ken

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>