简体   繁体   English

使用xslt 1.0从xml中删除属性,并使其值逗号分隔

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

This is my xml that is generated from temp table 这是我从临时表生成的xml

 SELECT DISTINCT *     
     FROM #tblCustomer Lines
     ORDER BY CustomerNumber
     FOR XML PATH('CustomerDetails') 

generated xml: 生成的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. 我想使用xslt删除所有元素,并使其以逗号分隔。

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 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 /> . number属性通过<xsl:number />在XML文件中使用CustomerDetails的索引。 So it is different from the CustomerNumber . 因此它不同于CustomerNumber

Output: 输出:

<Lines>
    <Line number="1">1,Active,Individual,Mr</Line>
    <Line number="2">2,Active,Individual,Miss</Line>
</Lines>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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