MSXML XHTML 和嵌入式 CSS

发布于 2024-09-07 03:55:20 字数 1406 浏览 5 评论 0原文

我有一个 Excel 工作簿,用作在我们的内部系统中生成用户可填写的表单的起点。作为创建这些工作簿的用户的助手,我尝试添加一个预览功能,该功能使用该电子表格,执行一些 VBA 魔法来生成 HTML 文件,然后将其显示在浏览器中。

我已经完成了基本结构,使用 MSXML 编写 XHTML,到目前为止,一切顺利。现在,我遇到了嵌入样式表的问题。

样式表包含在 VBA 代码中的字符串中,我尝试将其添加到标题中的

tl;dr: 如何将包含 > 的样式表嵌入到使用 MSXML 生成的 HTML 文件中?

编辑:这是重现此行为的代码块。将其放入 Excel 中的 Sub 或您选择的使用 VBA 的程序中,运行它并查看源代码:

Dim doc As DOMDocument
Dim htmlRoot As IXMLDOMElement
Dim bodyRoot As IXMLDOMElement
Dim headRoot As IXMLDOMElement
Dim style As IXMLDOMElement

Set doc = New DOMDocument
Set htmlRoot = doc.createElement("html")
Set bodyRoot = doc.createElement("body")
Set headRoot = doc.createElement("head")

Set style = doc.createElement("style")
style.appendChild doc.createTextNode(".section>.title{font-weight: bold;}")
style.setAttribute "type", "text/css"

headRoot.appendChild style

htmlRoot.appendChild headRoot
htmlRoot.appendChild bodyRoot

doc.appendChild htmlRoot

Dim fs As FileSystemObject
Dim sh
Dim tempFolder As String
Set fs = New FileSystemObject
Set sh = CreateObject("WScript.Shell")
tempFolder = fs.GetSpecialFolder(TemporaryFolder)

Dim fileName As String
fileName = tempFolder + "\preview.html"
doc.Save fileName
sh.Run fileName

I have an Excel workbook that is used as a starting point to generate a user-fillable form in our internal system. As an aide to the users creating these workbook, I'm trying to add a preview function, that takes that spreadsheet, does some VBA magic to generate an HTML file, and then display that in their browser.

I have the basic structure done, using MSXML to write out XHTML, so far, so good. Now, I'm hitting an issue with embedding the style sheet.

The style sheet is contained in a string in the VBA code, and I'm trying to add it into a <style> tag in the header, which is straightforward. Where I'm having the issue is I'm using CSS selectors in the stylesheet, with > causing me issues, since MSXML wants to encode that as a XML escape sequence, breaking the CSS. I've tried adding the stylesheet within a CDATA block, but then the browser just ignores it.

tl;dr: How can I embed a stylesheet containing > into an HTML file generated with MSXML?

EDIT: Here's a block of code that reproduces this behavior. Put it into a Sub in Excel or a VBA-using program of your choice, run it, and view the source:

Dim doc As DOMDocument
Dim htmlRoot As IXMLDOMElement
Dim bodyRoot As IXMLDOMElement
Dim headRoot As IXMLDOMElement
Dim style As IXMLDOMElement

Set doc = New DOMDocument
Set htmlRoot = doc.createElement("html")
Set bodyRoot = doc.createElement("body")
Set headRoot = doc.createElement("head")

Set style = doc.createElement("style")
style.appendChild doc.createTextNode(".section>.title{font-weight: bold;}")
style.setAttribute "type", "text/css"

headRoot.appendChild style

htmlRoot.appendChild headRoot
htmlRoot.appendChild bodyRoot

doc.appendChild htmlRoot

Dim fs As FileSystemObject
Dim sh
Dim tempFolder As String
Set fs = New FileSystemObject
Set sh = CreateObject("WScript.Shell")
tempFolder = fs.GetSpecialFolder(TemporaryFolder)

Dim fileName As String
fileName = tempFolder + "\preview.html"
doc.Save fileName
sh.Run fileName

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

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

发布评论

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

评论(2

日暮斜阳 2024-09-14 03:55:20

也许您可以使用现有方法生成 XHTML,而不是尝试存储将被解释为 XML 标记的 CSS 字符,但不是使用 MSXML 插入 CSS,而是插入占位符,并在完成构建 XHTML 后替换它。像这样:

style.appendChild doc.createTextNode("{css}")

' some more XHTML building here.

Dim html As String
Dim css As String

css = ".section>.title{font-weight: bold;}"
html = Replace(doc.Text, "{css}", css)

' Save the html here...

这样,您就可以在 XHTL 中嵌入您想要的任何内容,而不必担心 MSXML 试图为您转义它。

Perhaps instead of trying to store CSS characters which will be interpreted as XML markup, you could produce the XHTML using your existing method, but instead of inserting the CSS using MSXML, insert a placeholder, and replace it after you have finished building the XHTML. Something like this:

style.appendChild doc.createTextNode("{css}")

' some more XHTML building here.

Dim html As String
Dim css As String

css = ".section>.title{font-weight: bold;}"
html = Replace(doc.Text, "{css}", css)

' Save the html here...

This way, you can embed whatever you want in the XHTL, without having to worry about MSXML trying to escape it for you.

波浪屿的海角声 2024-09-14 03:55:20

XML 转义序列
>
是> (意思是大于)

The XML escape sequence for
>
is > (meaning GreaterThan)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文