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

Я работаю над xsl, чтобы преобразовать файлы карт памяти в древовидную структуру csv. Я использую питон 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>

person groovehunter    schedule 18.11.2012    source источник


Ответы (2)


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

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

...к этому:

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>
person ABach    schedule 18.11.2012
comment
Я не понимаю: что что дает отсутствующую корневую ошибку? - person ABach; 20.11.2012

Я не могу воспроизвести полученный результат — с 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>
person Dimitre Novatchev    schedule 18.11.2012
comment
поэтому я предполагаю, что проблема в том, что lxml не соответствует требованиям? Я спрошу об этом проекте позже. - person groovehunter; 19.11.2012