2012-11-18 12 views
4

我使用xsl将mentalmap文件转换为csv树结构。我使用python lxmlxslt模板根标签,在文本输出中省略

还有一点问题 - 如何省略模板中必要的根标记?让他们远离结果:

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> 

回答

1

更改此模板:

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

......这样:

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

这就是给出了“失踪根”错误... – groovehunter

+0

我不明白: *什么*是什么给“缺少根”的错误? – ABach

0

我不能瑞普报告的结果 - 与撒克逊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> 
+0

所以我假设问题是lxml不符合?我稍后会问这个项目。 – groovehunter