用于动态XML动态嵌套的XSLT

时间:2011-09-13 14:42:07

标签: xslt

我有一个动态创建XML文档的脚本。这意味着孩子的深度是未知的。当孩子级别未知时,是否可以为动态XML文档创建XSLT?

示例#1:

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

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

输出#1:

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

示例#2(儿童改变):

<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>

输出#2:

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

1 个答案:

答案 0 :(得分:1)

这应该会给你想要的结果:

<?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>