Create a list of element from nested elements in xslt?

时间:2015-10-06 08:37:21

标签: xslt-2.0

I have a document with nested elements.Now I want to list them one element after another element using XSLT 2.0

Here is the input:

<?xml version="1.0" encoding="UTF-8"?>
<a>
    <b>
        test text
        <b>
            this text is in b
        </b>
    </b>
    <b>
        this text is 
        <b>
            this text is test
        </b>
    </b>
</a>

This is what I expect:

<a>
    <b>test text</b>
    <b>this text is in b</b>
    <b>this text is </b>
    <b>this text is test</b>
</a>

I have no any idea to do this..I tried grouping concept..but it was not succeeded.Please help me to resolve this.

1 个答案:

答案 0 :(得分:0)

试试这个

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="b[b]">
        <xsl:copy>
            <xsl:apply-templates select="text()"/>
        </xsl:copy>
        <xsl:apply-templates select="*"/>
    </xsl:template>    

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

</xsl:stylesheet>