Reputation: 596
This is my xml that is generated from temp table
SELECT DISTINCT *
FROM #tblCustomer Lines
ORDER BY CustomerNumber
FOR XML PATH('CustomerDetails')
generated xml:
<CustomerDetails>
<CustomerNumber>1</CustomerNumber>
<Status>Active</Status>
<CustomerType>Individual</CustomerType>
<Title>Mr</Title>
</CustomerDetails>
<CustomerDetails>
<CustomerNumber>2</CustomerNumber>
<Status>Active</Status>
<CustomerType>Individual</CustomerType>
<Title>Miss</Title>
</CustomerDetails>
I want to use xslt to remove all the elements and make it comma separated.
Expected output:
<Lines>
<Line Number="1" Data="1,Active,Individual,Mr" />
<LineNumData="2" Data="2,Active,Individual,Mr" />
</Lines>
Any help would be highly appreciated. TIA
Upvotes: 0
Views: 39
Reputation: 29042
This solution differs a bit from your mentioned output, but I guess you can go on from here by yourself to make it fit your exact needs. To achieve the below output you can use these two templates
<xsl:template match="/">
<Lines>
<xsl:apply-templates select="node()|@*" />
</Lines>
</xsl:template>
<xsl:template match="CustomerDetails">
<Line>
<xsl:attribute name="number"><xsl:number /></xsl:attribute>
<xsl:value-of select="concat(CustomerNumber,',',Status,',',CustomerType,',',Title)" />
</Line>
</xsl:template>
The number
attribute uses the index of the CustomerDetails
in the XML file via <xsl:number />
. So it is different from the CustomerNumber
.
Output:
<Lines>
<Line number="1">1,Active,Individual,Mr</Line>
<Line number="2">2,Active,Individual,Miss</Line>
</Lines>
Upvotes: 1