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 January 12th, 2010, 03:20 PM
Authorized User
 
Join Date: Jan 2010
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
Default Howto produce nested elements from claset format

Hello,

I am new to this forum and hope to place my question here at the right place. What I try to do is a transformation of data from Eurostat (Claset-format) to a nested syle to use some common editors to view the data an easy way. The minimized problem:

Input data:

Code:
<?xml version="1.0" encoding="utf-8"?>
<items>
   <item id="i1" level="1" />
   <item id="i2" level="2" />
   <item id="i3" level="2" />
   <item id="i4" level="3" />
   <item id="i5" level="3" />
   <item id="i6" level="1" />
   <item id="i7" level="2" />
</items>
The @levels up between direct neighbors there is only one step alowed
For @levels down: all steps allowed (i.e. i5->i6)

Data should be transformed to 'nested structure': Required output

Code:
<Items>
   <Item id="i1">
      <Item id="i2" />
      <Item id="i3">
         <Item id="i4" />
         <Item id="i5" />
      </Item>
   </Item>
   <Item id="i6">
      <Item id="i7" />
   </Item>
</Items>

The transformation (with saxon9he.jar):

Code:
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/">
   <Items>
      <xsl:apply-templates select="//item[@level=1]"/>
   </Items>
</xsl:template>

<xsl:template match="*">
   <Item id="{@id}" level="{@level}">
      <xsl:variable name="level"><xsl:value-of select="@level" /></xsl:variable>
      <xsl:apply-templates select="following-sibling::item[@level = $level + 1]"/>
   </Item>
</xsl:template>

</xsl:transform>

The result is preety close to the required output. But it is faulty.

Code:
<Items>
   <Item id="i1" level="1">
      <Item id="i2" level="2">
         <Item id="i4" level="3"/>
         <Item id="i5" level="3"/>
      </Item>
      <Item id="i3" level="2">
         <Item id="i4" level="3"/>
         <Item id="i5" level="3"/>
      </Item>
      <Item id="i7" level="2"/>
   </Item>
   <Item id="i6" level="1">
      <Item id="i7" level="2"/>
   </Item>
</Items>
I presume the error is in the filter of the XPath expression "following-sibling::item[@level = $level + 1]".

How can I adress only those following-siblings down to where the attribute (@level) changes?

Any help very much apreciated.

Regards

Christian
(sorry for my english)
 
Old January 12th, 2010, 04:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You will find a paper that describes how to solve this classic problem here:

http://www.saxonica.com/papers/ideadb-1.1/mhk-paper.xml
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
CHRD (January 13th, 2010)
 
Old January 13th, 2010, 08:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample XSLT 2.0 stylesheet that uses xsl:for-each-group starting-with:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  version="2.0"
  xmlns:mf="http://example.com/2010/mf"
  exclude-result-prefixes="xsd mf">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="items">
    <xsl:copy>
      <xsl:sequence select="mf:nest(item, 1)"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:function name="mf:nest" as="element()*">
    <xsl:param name="items" as="element()*"/>
    <xsl:param name="level" as="xsd:integer"/>
    <xsl:for-each-group select="$items" group-starting-with="item[@level = $level]">
      <item id="{@id}">
        <xsl:sequence select="mf:nest(current-group() except ., $level + 1)"/>
      </item>
    </xsl:for-each-group>
  </xsl:function>
  
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
CHRD (January 13th, 2010)
 
Old January 13th, 2010, 04:02 PM
Authorized User
 
Join Date: Jan 2010
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank You very much!

Although it will take me some more hours to fully understand the code, I would like to post the final script. As mentioned before the scropt should transform hierarchical data from the european classification server to a easy readable format.

Source of the Claset files (Eurostat Classification in Claset format: click on the classification -> further files -> XML. Not available for all dimensions.):

http://ec.europa.eu/eurostat/ramon/n...getUrl=LST_NOM

Transformation (for collection of files in in-folder to be written in out folder)

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:transform 
   version="2.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns="http://www.ergonotes.com/XMLSchema/v2.xsd"
   xsi:schemaLocation="http://www.ergonotes.com/XMLSchema/v2.xsd http://www.ergonotes.com/XMLSchema/v2.xsd" 
   xmlns:mf="http://example.com/2010/mf"
   exclude-result-prefixes="xsd mf">

<xsl:output 
   method="xml" 	
   encoding="utf-8"
   indent="yes"/>

<xsl:template match="/">
<xsl:for-each select="collection(iri-to-uri('./in/?select=*.(xml)'))">
   <xsl:result-document href="./out/{encode-for-uri(tokenize(document-uri(.), '/')[last()])}.ergo" indent="yes">
   <ergonotes>
      <nodes>
         <xsl:sequence select="mf:nest(Claset/Classification/Item, 1)"/>
      </nodes>
   </ergonotes>
   </xsl:result-document>
</xsl:for-each>
</xsl:template>

<xsl:function name="mf:nest" as="element()*">
   <xsl:param name="items" as="element()*"/>
   <xsl:param name="level" as="xsd:integer"/>
   <xsl:for-each-group select="$items" group-starting-with="Item[@idLevel = $level]">
      <node guid="00000000-0000-0000-0000-000000000000" expanded="false" image="221">
         <xsl:attribute name="name"><xsl:value-of select="normalize-space(string-join((Label/LabelText),' '))" /></xsl:attribute>
         <xsl:value-of select="@id" />
         <xsl:sequence select="mf:nest(current-group() except ., $level + 1)"/>
      </node>
   </xsl:for-each-group>
</xsl:function>

</xsl:transform>
The transformation should easly be adopted to other kind of hierarchical editors or graph languages (dot). This one was done for ErgoNotes.
 
Old January 14th, 2010, 07:38 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You could shorten
Code:
   <xsl:for-each-group select="$items" group-starting-with="Item[@idLevel = $level]">
      <node guid="00000000-0000-0000-0000-000000000000" expanded="false" image="221">
         <xsl:attribute name="name"><xsl:value-of select="normalize-space(string-join((Label/LabelText),' '))" /></xsl:attribute>
         <xsl:value-of select="@id" />
         <xsl:sequence select="mf:nest(current-group() except ., $level + 1)"/>
      </node>
   </xsl:for-each-group>
to
Code:
   <xsl:for-each-group select="$items" group-starting-with="Item[@idLevel = $level]">
      <node guid="00000000-0000-0000-0000-000000000000" expanded="false" image="221" name="{normalize-space(string-join((Label/LabelText),' '))}">
         <xsl:value-of select="@id" />
         <xsl:sequence select="mf:nest(current-group() except ., $level + 1)"/>
      </node>
   </xsl:for-each-group>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem nested elements Geierwally XSLT 7 May 9th, 2007 07:27 AM
path for nested elements rjonk XSLT 7 November 20th, 2006 05:43 AM
sorting nested elements again stekker XSLT 1 June 5th, 2006 03:38 AM
sorting nested elements stekker XSLT 5 June 5th, 2006 01:19 AM
HOWTO Nested Grid in ASP.Net 2.0??? ziaiz Classic ASP Professional 0 April 13th, 2006 04:28 AM





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