views:

96

answers:

1

I have a xml document on the following format and want to transform it using a xsl template.

I'm a beginner at xsl transformations and I only need to know how to recurse trough the tree but a solution to the whole problem would be nice.

This is the xml document:

<?xml version="1.0" encoding="UTF-8" ?>
<nodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <node>
        <type>Parent</type>
        <name>.test</name>
        <node>
            <type>parent</type>
            <name>.test.root</name>
            <node>
                <type>Parent</type>
                <name>.test.root.group</name>
                <node>
                    <type>int</type>
                    <name>.test.root.group.a</name>
                    <value>0</value>
                </node>
                <node>
                    <type>char</type>
                    <name>.test.root.group.b</name>
                    <value>-</value>
                </node>
            </node>
        </node>
        <node>
            <type>parent</type>
            <name>.test.versions</name>
            <node>
                <type>utf-8</type>
                <name>.test.versions.version</name>
                <value>alpha</value>
            </node>
            <node>
                <type>utf-8</type>
                <name>.test.version.extra</name>
                <value>16.5</value>
            </node>
        </node>
    </node>
</nodes>

And this is how I would like the produced html to look like:

    .---------------------------------------------.
    | tree                   | value     | type   |
    |------------------------+-----------+--------|
    | '- test                |           | parent |
    |    |- root             |           | parent |
    |    |  '- group         |           | parent |
    |    |     |- a          | 0         | int    |
    |    |     '- b          | -         | char   |
    |    '- versions         |           | parent |
    |       |- version       | "alpha"   | utf-8  |
    |       '- extra         | 16.5      | utf-8  |
    '---------------------------------------------'
+2  A: 

This XSLT will generate a tree like you want:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="nodes/node">
      <xsl:with-param name="indent" select="''" />
      <xsl:with-param name="parent" select="''" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="node">
    <xsl:param name="indent"/>
    <xsl:param name="parent"/>

    <xsl:value-of select="$indent" />
    <xsl:value-of select="substring-after(name/text(), $parent)" />
    <xsl:text>&#xa;</xsl:text>

    <xsl:apply-templates select="./node">
      <xsl:with-param name="indent" select="concat($indent, '    |')" />
      <xsl:with-param name="parent" select="name/text()" />
    </xsl:apply-templates>

  </xsl:template>

</xsl:stylesheet>

Adding data to next two columns is pretty simple, try to do it by yourself.

darja