xsl-list
[Top] [All Lists]

Re: Convert xml to another xml structure.

2005-11-16 23:28:46
Hi Raj,

Is it possible to arrange this XML to the following
XML element using XSL and using level attributes.

Short answer: Yes.

I suppose that the real XML is wellformed....

<xsl:template match="/">
  <!-- This solution assumes that *[(_at_)level='1'] is the root of the 
document -->
  <xsl:apply-templates select="*[(_at_)level='1']">
</xsl:template>

<xsl:template match="*">
  <xsl:param name="current-level" select="'1'"/>
  <xsl:variable name="current-id" select="generate-id()"/>
  <xsl:copy>  <!-- Shallow copy of the current node -->
  <xsl:copy-of select="@*"/>  <!-- Copy all the attributes -->
  <!-- Get the logical children (will be called recursively): -->
  <xsl:apply-templates
select="following-sibling::*[(_at_)level=$current-level+1][generate-id(preceding-sibling::*[(_at_)level=$current-level])=$current-id]"><xsl:with-param
name="current-level" select="$current-level+1"/></xsl:apply-templates>
  </xsl:copy>
</xsl:template>

If you change the elements name so that they are the same (say X),
then you can use X instead of * in this solution. This will make the
templates more specific.
This solution, I think, does not have anything that has not been
discussed with you before in various posts.

First predicate ([...]) ensures that the element in question will be
at a level deeper that the current one.
Second predicate ([...]) ensures that the template will not call
children of a sibling parent.

Others on this list have suggested that you try out some of the
tutorials that exists on the net. If you can get hold of "XSLT,
programmers reference (2nd ed)" by Michael Kay, I think there is an
example of this problem in that book. It is a great book for general
understanding XSL as well as the reference that its title says it is.

Regards,
Ragulf Pickaxe :-)

--~------------------------------------------------------------------
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>
--~--