Reputation: 5
I am currently working on transform xml to csv, in the csv should have two row, first row are all the tag names from the xml (tags with data in it), the second row are all data matched to the first row tags. I have two separate xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="//text()"/>
</xsl:template>
<xsl:template match="text()">
<xsl:copy-of select="."/>
<xsl:if test="not(position()=last())">,</xsl:if>
</xsl:template>
</xsl:stylesheet>
this one can transform xml to csv of what I am expecting the second row data.
I have the following xsl can grab all the tags from the xml, but I only wanted the tags with data, like <Languages><Lanuage1>English</Language1><Lanuage2>French</Language2></Languages>
I only need the tags Language1 and Language2
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
<xsl:if test="not(position()=last())">,</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Can anyone solve the tag problem above and make a combination of these two xsl.
Thanks.
Upvotes: 0
Views: 225
Reputation: 1034
How about:
<?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"/>
<xsl:template match="/">
<xsl:variable name="nodes" select="//*[normalize-space(text()) != '']"/>
<xsl:for-each select="$nodes">
<xsl:value-of select="local-name()"/>
<xsl:if test="not(position()=last())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:for-each select="$nodes">
<xsl:value-of select="."/>
<xsl:if test="not(position()=last())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1