开发者

XSLT for dynamic XML Dynamic Nesting

I have a script that creates XML documents dynamically. That means the children-depth is unknown. Is it possible to create XSLT for a dynamically XML document when children level is unknown?

Example#1:

<root>
    <object type="set">
        <name>Test1</name>
        <object type="set">
            <name>Test11</name>
        </object>
    </object>

    <object type="set">开发者_开发问答
        <name>Test2</name>
    </object>
</root>

Output#1:

<html>
    <body>
        <div>Test1
            <div>Test11</div>
        </div>
        <div>Test2</div>
    </body>
</html>

Example#2 (children change):

<root>
    <object type="set">
        <name>Test1</name>
        <object type="set">
            <name>Test11</name>
            <object type="set">
                <name>Test111</name>
            </object>
        </object>
    </object>

    <object type="set">
        <name>Test2</name>
        <object type="set">
            <name>Test22</name>
        </object>
    </object>
</root>

Output#2:

<html>
    <body>
        <div>Test1
            <div>Test11
                <div>Test111</div>
            </div>
        </div>
        <div>Test2
            <div>Test22</div>
        </div>
    </body>
</html>


This should give you the desired results:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="object">
        <div>
            <xsl:value-of select="./name"/>
            <xsl:apply-templates />
        </div>
    </xsl:template>

    <xsl:template match="name" />

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜