Xml 递归xsl转换

Xml 递归xsl转换,xml,xslt,transformation,Xml,Xslt,Transformation,我有一个以下格式的xml文档,希望使用xsl模板对其进行转换 我是xsl转换的初学者,我只需要知道如何通过树进行递归,但是解决整个问题的方法会很好 这是xml文档: <?xml version="1.0" encoding="UTF-8" ?> <nodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <node> <type>Parent</type&

我有一个以下格式的xml文档,希望使用xsl模板对其进行转换

我是xsl转换的初学者,我只需要知道如何通过树进行递归,但是解决整个问题的方法会很好

这是xml文档:

<?xml version="1.0" encoding="UTF-8" ?>
<nodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <node>
        <type>Parent</type>
        <name>.test</name>
        <node>
            <type>parent</type>
            <name>.test.root</name>
            <node>
                <type>Parent</type>
                <name>.test.root.group</name>
                <node>
                    <type>int</type>
                    <name>.test.root.group.a</name>
                    <value>0</value>
                </node>
                <node>
                    <type>char</type>
                    <name>.test.root.group.b</name>
                    <value>-</value>
                </node>
            </node>
        </node>
        <node>
            <type>parent</type>
            <name>.test.versions</name>
            <node>
                <type>utf-8</type>
                <name>.test.versions.version</name>
                <value>alpha</value>
            </node>
            <node>
                <type>utf-8</type>
                <name>.test.version.extra</name>
                <value>16.5</value>
            </node>
        </node>
    </node>
</nodes>

父母亲
.测试
父母亲
.test.root
父母亲
.test.root.group
int
.test.root.group.a
0
烧焦
.test.root.b组
-
父母亲
.test.versions
utf-8
.test.versions.version
阿尔法
utf-8
.test.version.extra
16.5
我希望生成的html是这样的:

.---------------------------------------------. | tree | value | type | |------------------------+-----------+--------| | '- test | | parent | | |- root | | parent | | | '- group | | parent | | | |- a | 0 | int | | | '- b | - | char | | '- versions | | parent | | |- version | "alpha" | utf-8 | | '- extra | 16.5 | utf-8 | '---------------------------------------------' .---------------------------------------------. |树|值|类型| |------------------------+-----------+--------| |“-测试| |家长| ||-根| |父| ||'-组| |父| || |-a | 0 | int| ||'-b |-| char| |“-版本| |父级| ||-版本|“alpha”| utf-8| |'-extra | 16.5 | utf-8| '---------------------------------------------'
此XSLT将生成您想要的树:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="nodes/node">
      <xsl:with-param name="indent" select="''" />
      <xsl:with-param name="parent" select="''" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="node">
    <xsl:param name="indent"/>
    <xsl:param name="parent"/>

    <xsl:value-of select="$indent" />
    <xsl:value-of select="substring-after(name/text(), $parent)" />
    <xsl:text>&#xa;</xsl:text>

    <xsl:apply-templates select="./node">
      <xsl:with-param name="indent" select="concat($indent, '    |')" />
      <xsl:with-param name="parent" select="name/text()" />
    </xsl:apply-templates>

  </xsl:template>

</xsl:stylesheet>

将数据添加到下两列非常简单,请尝试自己添加。

此XSLT将生成您想要的树:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="nodes/node">
      <xsl:with-param name="indent" select="''" />
      <xsl:with-param name="parent" select="''" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="node">
    <xsl:param name="indent"/>
    <xsl:param name="parent"/>

    <xsl:value-of select="$indent" />
    <xsl:value-of select="substring-after(name/text(), $parent)" />
    <xsl:text>&#xa;</xsl:text>

    <xsl:apply-templates select="./node">
      <xsl:with-param name="indent" select="concat($indent, '    |')" />
      <xsl:with-param name="parent" select="name/text()" />
    </xsl:apply-templates>

  </xsl:template>

</xsl:stylesheet>

将数据添加到下两列非常简单,请尝试自己完成。

如果您不希望XML输出,为什么要使用XSL?请参阅更新(我希望输出格式像divs/html)@Oded:XSL除了输出XML之外还有其他用途。举几个例子,它可以像dacwe希望的那样输出为html,如果使用XSL-FO,它可以生成PDF文件,甚至还可以生成另一个XSL文档!有这么多的选择和能力。。它不仅限于生成xml:)如果您不想输出xml,为什么要使用XSL?请参阅更新(我希望输出的格式像divs/html)@Oded:XSL除了输出xml之外还有其他用途。举几个例子,它可以像dacwe希望的那样输出为html,如果使用XSL-FO,它可以生成PDF文件,甚至还可以生成另一个XSL文档!有这么多的选择和能力。。它不仅限于生成xml:)