Xslt template root tags, omit to text output

I am working on xsl to convert mindmap files to csv tree structure. I am using python lxml

There are few questions left - how to omit the root tags needed in the template? Leave them in the results:

AssertionError: ElementTree not initialized, missing root 

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> 

Output. Pay attention to p tags. How to reset 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> 
+4
source share
2 answers

Change this template:

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

... to that:

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

I can not reproduce the 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 compatible XSLT processor must comply with <xsl:output method="text"/> and only output text.

In any case, if you do not want to create 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> 
0
source

Source: https://habr.com/ru/post/1446822/


All Articles