tags:

views:

52

answers:

2
+1  Q: 

Stucked with xslt

Hi,

I"m new to xslt, and now I'm stucked a little with it.

I have an XML:

<map>
  <node TEXT="test" TYPE="node" >
    <element/>
    <node TYPE="normal" >
      <dummy>
        <FONT>
          mindmap_node1_l1
        </FONT>
      </dummy>
      <node LINK="url.com" TYPE="node" >
        <dummy>
          <FONT> 
            node node1_l2)
          </FONT>
        </dummy>
        <element/>
        <element/>
      </node>
    </node>
    <node TYPE="normal" >
      <dummy>
        <FONT>
          mindmap_node2_l1
        </FONT>
      </dummy>
      <node TYPE="normal" >
        <dummy>
          <FONT>
            mindmap_node3_l2
          </FONT>
        </dummy>
      </node>
    </node>
  </node>
</map>

I want it to convert something like that:

<?xml version="1.0"?>
<transform text="">
  <transform text="mindmap_node1_l1">
    <transform text="node node1_l2)">
    </transform>
  </transform>
  <transform text="mindmap_node2_l1">
    <transform text="mindmap_node3_l2">
    </transform>
  </transform>
</transform>

I wrote an xslt:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:template match="map">
    <xsl:apply-templates select="/map/node"/>
  </xsl:template>

  <xsl:template match="node">
    <xsl:element name="transform">
      <xsl:attribute name="text">
        <xsl:value-of select="normalize-space(.)"/>
      </xsl:attribute>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

But it really doesn't do what I want. What did I do wrong?

+1  A: 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="map">
    <xsl:apply-templates select="/map/node"/>
  </xsl:template>

  <xsl:template match="node">
    <xsl:element name="transform">
      <xsl:attribute name="text">
        <xsl:value-of select="normalize-space(dummy/FONT)"/>
      </xsl:attribute>
      <xsl:apply-templates select="node"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
icyfeel
+1  A: 

More compact, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="text()"/>
    <xsl:template match="node">
        <transform text="{normalize-space(dummy/FONT)}">
            <xsl:apply-templates />
        </transform>
    </xsl:template>
</xsl:stylesheet>

Output:

<transform text="">
    <transform text="mindmap_node1_l1">
        <transform text="node node1_l2)">
        </transform>
    </transform>
    <transform text="mindmap_node2_l1">
        <transform text="mindmap_node3_l2">
        </transform>
    </transform>
</transform>
Alejandro