reorganizing XML elements based on attributes
This is an open discussion with 6 replies, filed under XSLT.
Search
Maybe this is helpful?
Muenchian Grouping is a life-saver. (But it’s hard to wrap your mind around it.)
Everyone inevitably comes to Meunchian grouping when using XSL (at some point). This is one of those things that makes you stand back and realise just how powerful XSL is as a templating language.
If you have any troubles Ixai, post back here - I’m sure one of us can give you some advice.
Thanks! I think I understand how it works now. And as an aside I got to learn the quirks of <xsl:sort/>. The link mentions two methods for finding out whether a record is the first with a certain key value (the first with generate-id() and the second with count()). Does anyone know if there’s any notable performance difference between them?
The XSL for the example in this post (just in case…):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="categories" match="item" use="@category"/> <xsl:template match="/"> <root> <xsl:apply-templates/> </root> </xsl:template> <xsl:template match="root"> <xsl:for-each select="item[count(. | key('categories', @category)[1]) = 1]"> <!--<xsl:sort select="@category" data-type="number"/>--> <category id="{@category}"> <xsl:for-each select="key('categories', @category)"> <!--<xsl:sort select="@id" data-type="number"/>--> <item id="{@id}"/> </xsl:for-each> </category> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The performance will vary based upon the XSL interpreter - I don’t know what the figures are for the interpreter used by Symphony, but the generate-id method was always an order of magnitude faster under Xalan.
If you’re worried about performance at that level, try using the “Profile Devkit” extension to time the two different methods - I’d love to hear what you find.
Symphony uses GNOME’s libxml since PHP’s LibXML module is based on it. I’m not so worried about performance right now since my dataset is rather small right now, but if I ever get the chance to test something larger I’d gladly share it with you.
Create an account or sign in to comment.
I have some problems with an XSLT that I can’t figure out (I have little XSLT experience), hopefully someone can help me out. I know it’s not Symphony-specific but this is the only forum I know that deals with XSL all the time.
How can you turn an XML from this:
To this:
I started with something like this:
but this obviously creates one category element for each item (duplicating categories with the same id)
I then tried to get only the categories with a distinct-values:
but this doesn’t seem to output anything (I’m testing against w3school’s “tryxslt” page with Opera 10.50 so I don’t know how reliable it is).
Thanks