创建嵌套html列表的cte

时间:2011-05-04 00:14:00

标签: sql common-table-expression nested-sets recursive-query

我有以下cte:

WITH cte AS
(
        SELECT 
            c.ParentIDNo,  
            c.Category,
            c.ChildCategory,
            c.WSWebProductNameIDNo, 
            c.IDNo, 
            0 AS Level,
            CAST('/' + c.Category AS VARCHAR(1000)) as CteName
        FROM WSWebCategory as c
        WHERE c.ParentIDNo IS NULL

        UNION ALL

        SELECT   
            t.ParentIDNo, 
            t.Category,
            t.ChildCategory, 
            t.WSWebProductNameIDNo,
            t.IDNo, 
            cte.Level + 1 AS Level,
            CAST(cte.CteName + '/' + t.ChildCategory AS VARCHAR(1000)) AS CteName
        FROM WSWebCategory t
        INNER JOIN cte ON t.ParentIDNo = cte.IDNo
    )
    SELECT *, REPLICATE('----', Level) + CteName as CteName FROM cte
    ORDER BY cte.CteName

给了我一个列表:

    /Apparatus
    ----/Apparatus/Autoclaves
    --------/Apparatus/Autoclaves/ALL-AMERICAN Portable Sterilizers (WAFCO)
    --------/Apparatus/Autoclaves/Autoclave Accessories
    ------------/Apparatus/Autoclaves/Autoclave Accessories/Clavies® Autoclave Gloves (Bel-Art Scienceware)
    ------------/Apparatus/Autoclaves/Autoclave Accessories/Grabbit™ Temp Mitts (Heathrow Scientific)
    ------------/Apparatus/Autoclaves/Autoclave Accessories/Odo-Clave® Deodorant Pads (Bel-Art Scienceware)
    --------/Apparatus/Autoclaves/Autoclave Bags
    ------------/Apparatus/Autoclaves/Autoclave Bags/Autoclavable Biohazard Disposal Bags (Bel-Art Scienceware)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Autoclavable Biohazard Disposal Bags (Gosselin)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Autoclavable Hi-Temp Biohazard Disposal Bags (Bel-Art Scienceware)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Chex-All® II Sterilization Pouches (Propper)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Clavies® Autoclave Gloves (Bel-Art Scienceware)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Clavies® Bag Holder (Bel-Art Scienceware)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Clavies® General Purpose Autoclavable Bags (Bel-Art Scienceware)
    ------------/Apparatus/Autoclaves/Autoclave Bags/Poxygrid® Biohazard Bag Holder (Bel-Art Scienceware)

但我所追求的是一组嵌套的html列表:

<ul> 
<li>Apparatus
   <ul> 
    <li>Autoclaves
        <ul>   
            <li>ALL-AMERICAN Portable Sterilizers (WAFCO)</li> 
            <li>Autoclave Accessories
                <ul>
                    <li>Clavies® Autoclave Gloves (Bel-Art Scienceware)</li>
                    <li>Grabbit™ Temp Mitts (Heathrow Scientific)</li>
                    <li>Odo-Clave® Deodorant Pads (Bel-Art Scienceware) </li>
                </ul>
            </li>
            <li>Autoclave Bags
                <ul>
                    <li>Autoclavable Biohazard Disposal Bags (Bel-Art Scienceware)</li>
                    <li>Autoclavable Biohazard Disposal Bags (Gosselin)</li>
                    <li>Autoclavable Hi-Temp Biohazard Disposal Bags (Bel-Art Scienceware)</li>
                    <li>Chex-All® II Sterilization Pouches (Propper)</li>
                    <li>Clavies® Autoclave Gloves (Bel-Art Scienceware)</li>
                    <li>Clavies® Bag Holder (Bel-Art Scienceware)</li>
                    <li>Clavies® General Purpose Autoclavable Bags (Bel-Art Scienceware)</li>
                    <li>Poxygrid® Biohazard Bag Holder (Bel-Art Scienceware)</li>
                </ul>
            </li>
        </ul>
    </li> 
    </ul>
</li>
</ul> 

我很难过如何做到这一点!

2 个答案:

答案 0 :(得分:0)

是否有某种原因你不能使用你的更高级编程语言在背面解析它?

看起来你的数据是XML输出的一个很好的候选者(使用SELECT ... FOR XML EXPLICIT。)你的数据看起来很完美,可以很容易地被应用程序端的东西解析。

除此之外,您可能会使用结果的连接添加列表项标签,​​并在您在扩展文本上阅读之前在下一个CTE循环中剥离标记。结束标签将成为挑战,因为我不相信CTE以递归的方式退出,允许你关闭它们。

答案 1 :(得分:0)

四年后......

伪代码:

previousLevel = -1;

for each item in items

  if item.level > previousLevel
    print "<ul>"

  if item.level < previousLevel
    print repeat("</ul>", previousLevel - item.level)

  print "<li>" + item.text + "</li>"

  previousLevel = item.level

end for

print repeat("</ul>", previousLevel + 1)

JSP / JSTL:

<c:set var="previousLevel" value="-1"/>

<c:forEach items="${items}" var="item">

    <c:if test="${ item.level > previousLevel }">
        <ul>
    </c:if>

    <c:if test="${ item.level < prevLevel }">
        ${xfn:repeat("</ul>", previousLevel - item.level)}
    </c:if>

    <li>${item.text}</li>

    <c:set var="previousLevel" value="${item.level}"/>

</c:forEach>

<c:if test="${previousLevel > -1}">
    ${xfn:repeat("</ul>",  previousLevel + 1 )}
</c:if>

其中xfn:repeat是任何字符串重复函数的tld函数