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 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧