简体   繁体   English

MSXML XHTML和嵌入式CSS

[英]MSXML XHTML and embedded CSS

I have an Excel workbook that is used as a starting point to generate a user-fillable form in our internal system. 我有一个Excel工作簿,用作在内部系统中生成用户可填写的表单的起点。 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. 作为创建这些工作簿的用户的助手,我正在尝试添加一个预览功能,该功能使用该电子表格,执行一些VBA魔术以生成HTML文件,然后在其浏览器中显示该文件。

I have the basic structure done, using MSXML to write out XHTML, so far, so good. 我已经完成了基本结构,使用MSXML编写了XHTML,到目前为止,一切都很好。 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. 样式表包含在VBA代码的字符串中,我试图将其添加到标题的<style>标记中,这很简单。 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. 我遇到问题的地方是我在样式表中使用CSS选择器,而>引起了我的问题,因为MSXML希望将其编码为XML转义序列,从而破坏CSS。 I've tried adding the stylesheet within a CDATA block, but then the browser just ignores it. 我试过在CDATA块中添加样式表,但是浏览器只是忽略了它。

tl;dr: How can I embed a stylesheet containing > into an HTML file generated with MSXML? tl; dr:如何将包含>的样式表嵌入到由MSXML生成的HTML文件中?

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: 将其放入您选择的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

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. 也许不用尝试存储将被解释为XML标记的CSS字符,而是可以使用现有方法来生成XHTML,而不是使用MSXML插入CSS,而是插入一个占位符,并在完成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. 这样,您可以在XHTL中嵌入任何内容,而不必担心MSXML试图为您转义它。

The XML escape sequence for > is &gt; >的XML转义序列是&gt; (meaning GreaterThan) (表示大于)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM