创建嵌套 html 列表的 cte
我有以下 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>
我很困惑如何做到这一点!
I have the following 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
which gives me a list like:
/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)
but what I'm after is a set of nested html lists:
<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>
I'm stumped as how to do this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否有某种原因您不能使用任何高级编程语言在背面解析它?
看来您的数据是 XML 输出的绝佳候选者(使用 SELECT ... FOR XML EXPLICIT。)您的数据似乎为此而完美设置,这可以很容易地被应用程序端的某些内容解析。
除此之外,您可能会使用结果串联来添加列表项标签,并在下一个 CTE 循环中剥离标签,然后再在扩展文本上读取它。不过,结束标签将是一个挑战,因为我不相信 CTE 会以递归方式退出以允许您关闭它们。
Is there some reason you can't just parse it on the backside using whatever your higher-level programming language is?
It would appear you data is an excellent candidate for XML output (using SELECT ... FOR XML EXPLICIT.) Your data seems set up perfectly for that, which would be easily parsed by something on your application side.
Outside of that, you could likely get the list item tags added using concatenation of the results and stripping the tag in the next CTE loop before you read it on the extended text. The closing tags are going to be the challenge though, as I don't believe CTE's step back out in a recursive way to allow you to close them out.
四年后...
伪代码:
JSP / JSTL:
其中
xfn:repeat
是任何 String 重复函数的 tld 函数Four years later...
Pseudocode:
JSP / JSTL:
Where
xfn:repeat
is a tld function for any String repeat function