[xslt] Flattenning XML
- From: "Sabu Francis (Personal)" <sf sabufrancis com>
- To: The Gnome XSLT library mailing-list <xslt gnome org>
- Subject: [xslt] Flattenning XML
- Date: Sun, 09 Jan 2005 14:05:24 +0530
Hi:
I would appreciate if I can get some help on an XSLT transformation.
I need to flatten an XML tree which can be of any structure with any
number of elements. (The element names in the original XML are not
known) The clue to flattening it is an attribute, say 'flatten' and on
its presence in the element (flatten="yes") the XSLT transformation
should isolate that tree fragment and take it out of the parent tree and
flatten it further. Individual pieces of the element data are collected
into our own element called a "snip"
<aa flatten="yes">
This is a test
<bb flatten="yes">
of things
</bb>
that can can be done in XSLT
</aa>
should flatten to
<topic elemname="aa" id="pqrs" parentID="nil" >
<snip id="xyz" parentID="pqrs" >This is a test</snip>
<snip id="123" parentID="pqrs">that can be done in XSLT</snip>
</topic>
<topic elemname="bb" id="zzz" parentID="xyz">
<snip id="hhh" parentID="zzz">of things</snip>
</topic>
As you can see, though the structure has been flattened out, I can still
recreate the old XML file because the nested structure has been passed
along via the attributes "id" and "parentID". The name of the original
element is also passed along. They can be used to walk up the nested
structure back again.
Now I've been able to achieve this partially (i.e. it works on certain
kinds of XML files) but I need this to work in ALL situations.
The key portion of the XSLT I used was this:
<xsl:template name="deepCpy">
<xsl:for-each select="descendant-or-self::*/text()">
<xsl:choose>
<xsl:when test="not(../@flatten )">
<xsl:call-template name="applysnip" /> <!--the template that does
the creating of the snip elements -->
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
In the above, it is obvious that the test will pass only when the
attribute 'flatten' is not present. But then such a test will detect
ONLY those elements where the "flatten" attribute is given. It will not
detect those elements that are not direct children of elements which
have the "flatten" attribute
What else can be done?
Regards
Sabu
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]