编写 XSLT 样式表 Header

Writing an XSLT Stylesheet Header

我正在构建一个 C# 应用程序,它将基于 pre-configured 文件为我构建 XSLT 文件。

我可以生成 XSLT 文件,它接近我想要的,但我有几个问题。

问题 1:
XSLT 文件顶部的样式表 header 格式很奇怪。这是我期待的:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">

这是我得到的:

<xsl:stylesheet p1:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" p3:o="urn:schemas-microsoft-com:office:office"
p3:x="urn:schemas-microsoft-com:office:excel" p3:ss="urn:schemas-microsoft-com:office:spreadsheet" p3:html="http://www.w3.org/TR/REC-html40" xmlns:p3="xmlns" xmlns:p1="stylesheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

这是 C# 代码:

//Write the namespaces for the xslt
        xmlWriter.WriteStartElement("xsl", "stylesheet", "http://www.w3.org/1999/XSL/Transform");
        xmlWriter.WriteAttributeString("xs", "stylesheet", "http://www.w3.org/2001/XMLSchema");
        xmlWriter.WriteAttributeString("exclude-result-prefixes", "xs");
        xmlWriter.WriteAttributeString("version", "1.0");
        xmlWriter.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
        xmlWriter.WriteAttributeString("o", "xmlns", "urn:schemas-microsoft-com:office:office");
        xmlWriter.WriteAttributeString("x", "xmlns", "urn:schemas-microsoft-com:office:excel");
        xmlWriter.WriteAttributeString("ss", "xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
        xmlWriter.WriteAttributeString("html", "xmlns", "http://www.w3.org/TR/REC-html40");

问题 2:
在我的 XSLT 文件的一般 body 中,这些 "p" 声明似乎出现在多个位置。在我上面的输出中,一个例子是:

p3:x="urn:schemas-microsoft-com:office:excel"

我想我在某种程度上错误地调用了该方法,但我不确定如何更正它。

似乎参数需要改变位置并以适当的方式使用。

参考:https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter.writeattributestring?view=netframework-4.7.2

你的情况应该写成

例如:

xmlWriter.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");

因为WriteAttributeString(String, String, String, String)

在派生 class 中覆盖时,写出具有指定前缀、本地名称、命名空间 URI 和值的属性。

public void WriteAttributeString (string prefix, string localName, string ns, string value);