Question

I work on a xsl to transform mindmap files to csv tree structure. I use python lxml

There is a little issue left - How to omit the root tags necessary in template ? Leave them away results in:

AssertionError: ElementTree not initialized, missing root

The source xml

<map version="0.9.0">

<node TEXT="Familie">
<node TEXT="Kinder">
<node TEXT="Sohn">
</node>

<node TEXT="Tochter">
<node TEXT="Hobbies">
<node TEXT="Fu&#xdf;ball">
</node>
</node>
</node>

</node>
</node>
</map>

The output. Notice the p tags. How to drop them??

<p>,"Familie"
  "Familie","Kinder"
  "Familie","Kinder","Sohn"
  "Familie","Kinder","Tochter"
  "Familie","Kinder","Tochter","Hobbies"
  "Familie","Kinder","Tochter","Hobbies","Fu&#223;ball"
  </p>

my sheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="ISO-8859-1" omit-xml-declaration="yes" media-type="string"/>
<xsl:strip-space elements="*" />

  <xsl:template match="/">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

<xsl:template match="node">
  <xsl:param name="par"/>
  <xsl:variable name="nodetext" select="@TEXT"/>
  <xsl:variable name="depth" select="count(ancestor::*)"/>
<xsl:value-of select="$par"/>,&quot;<xsl:value-of select="$nodetext"/>&quot;
  <xsl:choose>

    <xsl:when test="$depth&lt;2">
      <xsl:apply-templates>
        <xsl:with-param name="par" select="concat('&quot;',$nodetext,'&quot;')"/>
      </xsl:apply-templates>
    </xsl:when>

    <xsl:otherwise>
      <xsl:apply-templates>
        <xsl:with-param name="par" select="concat($par,',&quot;',$nodetext,'&quot;')"/>
      </xsl:apply-templates>
    </xsl:otherwise>

  </xsl:choose>

</xsl:template>

</xsl:stylesheet>

No correct solution

OTHER TIPS

Change this template:

<xsl:template match="/">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

...to this:

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

I cannot repro the reported result -- with Saxon 6.5.4 I get:

,"Familie"
  "Familie","Kinder"
  "Familie","Kinder","Sohn"
  "Familie","Kinder","Tochter"
  "Familie","Kinder","Tochter","Hobbies"
  "Familie","Kinder","Tochter","Hobbies","Fuޢall"

And any compliant XSLT processor should honor the <xsl:output method="text"/> and produce text-only output.

Anyway, if you don't want to produce any elements, remove them from the transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="ISO-8859-1" omit-xml-declaration="yes" media-type="string"/>
<xsl:strip-space elements="*" />

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

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

  <xsl:variable name="nodetext" select="@TEXT"/>
  <xsl:variable name="depth" select="count(ancestor::*)"/>

  <xsl:value-of select="$par"/>,&quot;<xsl:value-of select="$nodetext"/>&quot;

  <xsl:choose>
    <xsl:when test="$depth&lt;2">
      <xsl:apply-templates>
        <xsl:with-param name="par" select="concat('&quot;',$nodetext,'&quot;')"/>
      </xsl:apply-templates>
    </xsl:when>

    <xsl:otherwise>
      <xsl:apply-templates>
        <xsl:with-param name="par" select="concat($par,',&quot;',$nodetext,'&quot;')"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top