3

我有以下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

这给了我一个类似的列表:

    /仪器
    ----/仪器/高压灭菌器
    --------/仪器/高压灭菌器/ALL-AMERICAN 便携式灭菌器 (WAFCO)
    --------/仪器/高压灭菌器/高压灭菌器配件
    ------------/Apparatus/Autoclaves/Autoclave Accessories/Clavies® Autoclave Gloves (Bel-Art Scienceware)
    ------------/仪器/高压灭菌器/高压灭菌器配件/Grabbit™ Temp Mitts (Heathrow Scientific)
    ------------/设备/高压灭菌器/高压灭菌器配件/Odo-Clave® 除臭垫(Bel-Art Scienceware)
    --------/仪器/高压灭菌器/高压灭菌袋
    ------------/仪器/高压灭菌器/高压灭菌袋/高压灭菌生物危害处理袋(Bel-Art Scienceware)
    ------------/仪器/高压灭菌器/高压灭菌袋/高压灭菌生物危害处理袋(Gosselin)
    ------------/仪器/高压灭菌器/高压灭菌袋/可高压灭菌的高温生物危害处理袋(Bel-Art Scienceware)
    ------------/仪器/高压灭菌器/高压灭菌袋/Chex-All® II 灭菌袋(本体)
    ------------/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® 通用高压灭菌袋(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> 

我很困惑如何做到这一点!

4

2 回答 2

0

有什么原因你不能只用你的高级编程语言在背面解析它吗?

看起来您的数据是 XML 输出的绝佳候选者(使用 SELECT ... FOR XML EXPLICIT。)您的数据似乎为此完美设置,应用程序端的某些东西很容易解析这些数据。

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

于 2011-05-10T18:50:28.673 回答
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 函数在哪里

于 2015-04-21T00:17:55.940 回答