创建 HTML 表并使用 SQL FOR XML 指定字体大小

发布于 2024-12-21 13:17:55 字数 938 浏览 1 评论 0原文

我需要编写 SQL 语句来返回 html 表并指定其内容的字体大小。

我在此处找到了一些信息。本主题的解决方案描述了如何获取包含元素但不包含属性的 XML:

<tr>
    <th>Problem</th>
    <th>Onset</th>
    <th>Status</th>
</tr>
<tr>
    <td>aaa</td>
    <td>bbb</td>
    <td>ccc</td>
</tr>

但是我需要编写返回如下内容的 SQL 语句:

<tr>
    <th><font size="1">Problem</font></th>
    <th><font size="1">Onset</font></th>
    <th><font size="1">Status</font></th>
</tr>
<tr>
    <td><font size="1">aaa</font></td>
    <td><font size="1">bbb</font></td>
    <td><font size="1">ccc</font></td>
</tr>

I need to write SQL statement that will return an html table and specify font size to its content.

I've found some information here. Solusion of this tipic describes how to get XML with elements but without attributes:

<tr>
    <th>Problem</th>
    <th>Onset</th>
    <th>Status</th>
</tr>
<tr>
    <td>aaa</td>
    <td>bbb</td>
    <td>ccc</td>
</tr>

But I need to write SQL statement that would return something like this:

<tr>
    <th><font size="1">Problem</font></th>
    <th><font size="1">Onset</font></th>
    <th><font size="1">Status</font></th>
</tr>
<tr>
    <td><font size="1">aaa</font></td>
    <td><font size="1">bbb</font></td>
    <td><font size="1">ccc</font></td>
</tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-12-28 13:17:55

一些想法。

1) 在应用程序中而不是在查询中将 SQL 数据转换为 XML。 .NET / PHP / Java 都有方法将 SQL 数据获取为 XML。

2) 使用 XSL 将 XML 从数据库转换为 HTML

3) 考虑使用 CSS 代替 标签。

table td {
    FONT-SIZE: 12px;
}

A couple thoughts.

1) Convert your SQL data to XML in your application, not in the query. .NET / PHP / Java all have ways to get SQL data as XML.

2) Use XSL to transform the XML from the database to HTML

3) Consider using CSS instead of <font> tags.

table td {
    FONT-SIZE: 12px;
}
喵星人汪星人 2024-12-28 13:17:55
declare @T table
(
  ProblemType varchar(10),
  Onset date,
  DiagnosisStatus varchar(10)
)

insert into @T values
(  'Ulcer',     '01/01/2008',  'Active'),
(  'Edema',     '02/02/2005',  'Active')

select 
  (select 1 as 'th/@size', 'Problem' as th for xml path(''), type),
  (select 1 as 'th/@size', 'Onset'   as th for xml path(''), type),
  (select 1 as 'th/@size', 'Status'  as th for xml path(''), type)
union all         
select 
  (select 1 as 'td/@size', p.ProblemType     as 'td' for xml path(''), type),
  (select 1 as 'td/@size', p.Onset           as 'td' for xml path(''), type),
  (select 1 as 'td/@size', p.DiagnosisStatus as 'td' for xml path(''), type)
from @T p
for xml path('tr')

结果:

<tr>
  <th size="1">Problem</th>
  <th size="1">Onset</th>
  <th size="1">Status</th>
</tr>
<tr>
  <td size="1">Ulcer</td>
  <td size="1">2008-01-01</td>
  <td size="1">Active</td>
</tr>
<tr>
  <td size="1">Edema</td>
  <td size="1">2005-02-02</td>
  <td size="1">Active</td>
</tr>
declare @T table
(
  ProblemType varchar(10),
  Onset date,
  DiagnosisStatus varchar(10)
)

insert into @T values
(  'Ulcer',     '01/01/2008',  'Active'),
(  'Edema',     '02/02/2005',  'Active')

select 
  (select 1 as 'th/@size', 'Problem' as th for xml path(''), type),
  (select 1 as 'th/@size', 'Onset'   as th for xml path(''), type),
  (select 1 as 'th/@size', 'Status'  as th for xml path(''), type)
union all         
select 
  (select 1 as 'td/@size', p.ProblemType     as 'td' for xml path(''), type),
  (select 1 as 'td/@size', p.Onset           as 'td' for xml path(''), type),
  (select 1 as 'td/@size', p.DiagnosisStatus as 'td' for xml path(''), type)
from @T p
for xml path('tr')

Result:

<tr>
  <th size="1">Problem</th>
  <th size="1">Onset</th>
  <th size="1">Status</th>
</tr>
<tr>
  <td size="1">Ulcer</td>
  <td size="1">2008-01-01</td>
  <td size="1">Active</td>
</tr>
<tr>
  <td size="1">Edema</td>
  <td size="1">2005-02-02</td>
  <td size="1">Active</td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文