4

我在 xsl 上工作,将思维导图文件转换为 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>
4

2 回答 2

1

更改此模板:

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

...对此:

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>
于 2012-11-18T22:01:04.957 回答
0

我无法重现报告的结果——使用 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>
于 2012-11-18T22:34:08.543 回答