posted 22 years ago
Bala, if you need to output specific attributes or all attributes in specific order,
you can use
<xsl:value-of select="element/@attribute"/>
construction. For example:
<xsl:value-of select="catalog/cd/@title"/>
If you need to output all attributes in whatever order they are, you can write a generic template that will math all attributes:
<xsl:template match="@*">
<xsl:value-of select="."/><xsl:text>|</xsl:text>
</xsl:template>
To call it recursively it a bit tricky. You need to use <xsl:apply-templates> element:
<xsl:template match="*">
<xsl:apply-templates select="@*"/><xsl:apply-templates/>
</xsl:template>
This variant will output
Empire Burlesque|Bob Dylan|USA|Columbia|10.90|1985|1|2|3|
If you really need to get rid of the last "|" symbol, you can direct all the output to a variable, and then use substring() function:
<xsl:template match="/">
<xsl:variable name="attributes">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="substring ($attributes, 1, string-length($attributes)-1)"/>
</xsl:template>
The whole stylesheet will be:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:variable name="attributes">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="substring ($attributes, 1, string-length($attributes)-1)"/>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="@*"/><xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*">
<xsl:value-of select="."/><xsl:text>|</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>