【问题标题】:Create HTML Table and specify fontsize with SQL FOR XML创建 HTML 表并使用 SQL FOR XML 指定字体大小
【发布时间】:2011-12-16 21:44:46
【问题描述】:

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

我找到了一些信息here。这个tipic的解决方案描述了如何获取带有元素但没有属性的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>

【问题讨论】:

    标签: html sql xml font-size


    【解决方案1】:

    一些想法。

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

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

    3) 考虑使用 CSS 而不是 &lt;font&gt; 标签。

    table td {
        FONT-SIZE: 12px;
    }
    

    【讨论】:

    • 我正在考虑在我的 .NET 代码中使用字符串并将 标记直接插入到生成的 html 中。但这太复杂了,因为我的存储过程可以根据输入值返回不同格式的值,而使用 .NET 应用程序甚至不知道它收到了什么,它只知道这些数据可以显示在嵌入式浏览器中。样式表对我来说是一个非常好的解决方案!非常感谢!
    【解决方案2】:
    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>
    

    【讨论】:

    • 嗨,米凯尔·埃里克森!谢谢您的回答。当我第一次遇到需要更改字体大小时,我正在考虑通过 SQL FOR XML 创建元素和属性。我认为您的回答是我任务的直接解决方案。但之前的回答给了我比我预期的更多。谢谢大家!
    猜你喜欢
    • 2023-03-23
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多