dongzhuo0895 2011-04-19 07:57
浏览 59
已采纳

将多维数组转换为XML,然后使用递归XSLT函数

I have a function in PHP that retrieves all the directories and files from a given path. This returns me an array like:

array(
    "dirname1" => array(
        "dirname2" => array(
            "dirname3" => array(
                "0" => "file1",
                "1" => "file2",
                "2" => "file3"
            ),
            "0" => "file4",
            "1" => "file5",
            "2" => "file6",
            "dirname4" => array(
                "0" => "file7",
                "1" => "file8"
            )
        ),
        "0" => "file9",
        "1" => "file10"
    ),
    "0" => "file11",
    "1" => "file12",
    "2" => "file13"
);

What I finally need is a multidimensional (don't know if is the exactly word) list with <ul /> and <li /> generated with XSLT 1.0 from a XML file, like:

<ul>
    <li class="dirname">
        dirname1
        <ul>
            <li class="dirname">
                Dirname2
                <ul>
                    <li class="dirname">
                        Dirname3
                        <ul>
                            <li>file1</li>
                            <li>file2</li>
                            <li>file3</li>
                        </ul>
                    </li>
                    <li>file4</li>
                    <li>file5</li>
                    <li>file6</li>
                    <li class="dirname">
                        dirname4
                        <ul>
                            <li>file7</li>
                            <li>file8</li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>file9</li>
            <li>file10</li>
        </ul>
    </li>
    <li>file11</li>
    <li>file12</li>
    <li>file13</li>
</ul>

And finally, inside every <li /> I need the path in a <a />, like:

<li class="dirname"><a href="/dirname1">dirname1</a></li>

<li><a href="/dirname1/dirname2/dirname3/file1">file1</a></li>

<li><a href="/dirname1/file9">file9</a></li>

Actually I don't have the XML that I need to convert because I don't know what can be a nice structure for then convert it to XSLT 1.0. I have the paths inside the <a />. I can do it with PHP if necessary and I can detect in PHP when it is a directory or when not and we can also add something on the XML to detect the class="dirname".

I hope I've given sufficient information to understand me.

Thank you in advance!

  • 写回答

2条回答 默认 最新

  • dongshang1768 2011-04-19 15:40
    关注

    Tunneling parameter:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:strip-space elements="*"/>
        <xsl:template match="node()|@*" name="identity">
            <xsl:param name="pPath"/>
            <xsl:copy>
                <xsl:apply-templates select="node()|@*">
                    <xsl:with-param name="pPath" select="$pPath"/>
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="li[@class='dirname']">
            <xsl:param name="pPath"/>
            <xsl:call-template name="identity">
                <xsl:with-param name="pPath"
                 select="concat($pPath,'/',normalize-space(text()[1]))"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template match="text()">
            <xsl:param name="pPath"/>
            <xsl:call-template name="text">
                <xsl:with-param name="pPath"
                 select="concat($pPath,'/',normalize-space())"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template match="li[@class='dirname']/text()" name="text">
            <xsl:param name="pPath"/>
            <a href="{$pPath}">
                <xsl:value-of select="."/>
            </a>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <ul>
        <li class="dirname">
            <a href="/dirname1">
                dirname1
            </a>
            <ul>
                <li class="dirname">
                    <a href="/dirname1/Dirname2">
                        Dirname2
                    </a>
                    <ul>
                        <li class="dirname">
                            <a href="/dirname1/Dirname2/Dirname3">
                                Dirname3
                            </a>
                            <ul>
                                <li>
                                    <a href="/dirname1/Dirname2/Dirname3/file1"
                                    >file1</a>
                                </li>
                                <li>
                                    <a href="/dirname1/Dirname2/Dirname3/file2"
                                    >file2</a>
                                </li>
                                <li>
                                    <a href="/dirname1/Dirname2/Dirname3/file3"
                                    >file3</a>
                                </li>
                            </ul>
                        </li>
                        <li>
                            <a href="/dirname1/Dirname2/file4">file4</a>
                        </li>
                        <li>
                            <a href="/dirname1/Dirname2/file5">file5</a>
                        </li>
                        <li>
                            <a href="/dirname1/Dirname2/file6">file6</a>
                        </li>
                        <li class="dirname">
                            <a href="/dirname1/Dirname2/dirname4">
                                dirname4
                            </a>
                            <ul>
                                <li>
                                    <a href="/dirname1/Dirname2/dirname4/file7"
                                    >file7</a>
                                </li>
                                <li>
                                    <a href="/dirname1/Dirname2/dirname4/file8"
                                    >file8</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="/dirname1/file9">file9</a>
                </li>
                <li>
                    <a href="/dirname1/file10">file10</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="/file11">file11</a>
        </li>
        <li>
            <a href="/file12">file12</a>
        </li>
        <li>
            <a href="/file13">file13</a>
        </li>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里