Корневые теги шаблона xslt, опускаются при выводе текста

Я работаю над xsl, чтобы преобразовать файлы mindmap в древовидную структуру csv. Я использую python lxml

Осталась небольшая проблема - как опустить корневые теги, необходимые в шаблоне? Оставить их в результате приводит к:

AssertionError: ElementTree not initialized, missing root

Исходный 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>

Выход. Обратите внимание на теги p. Как их уронить??

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

мой лист

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

2 ответа

Изменить этот шаблон:

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

...к этому:

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

Я не могу воспроизвести сообщенный результат - с помощью Saxon 6.5.4 я получаю:

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

И любой совместимый XSLT-процессор должен соблюдать <xsl:output method="text"/> и производить только текстовый вывод.

В любом случае, если вы не хотите создавать какие-либо элементы, удалите их из преобразования:

<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>
Другие вопросы по тегам