简体   繁体   中英

Remove attributes from xml using xslt 1.0 and make it values comma separated

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM