【发布时间】:2012-01-14 06:03:33
【问题描述】:
背景:我正在尝试编写 Elmah 的配置脚本,以便在生产中运行一个脚本,该脚本将复制二进制文件并相应地编辑 web.config 文件。
我决定使用 XSLT 来更改 web.config 文件。首先,我想像这样在configSections 元素中添加一个sectionGroup。
<sectionGroup name="elmah">
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
....
</sectionGroup>
这是我的 XSLT 模板
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:con="http://schemas.microsoft.com/.NetConfiguration/v2.0"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Add sectionGroup to configuration/configSections-->
<xsl:template match="con:configSections[not (con:sectionGroup/@name='elmah')]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:comment>ELMAH</xsl:comment>
<sectionGroup name="elmah" >
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
<xsl:comment>/ELMAH</xsl:comment>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
它几乎可以满足我的要求。它会生成以下sectionGroup XML。
<!--ELMAH-->
<sectionGroup name="elmah" xmlns="" xmlns:con="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
....
</sectionGroup>
<!--/ELMAH-->
注意xmlns 属性。这个属性的存在确实让 ASP.Net 感到不安(即使它是完全有效的 XML——尽管是多余的)并且所有请求都会导致 HTTP 500 错误(请参阅下面的错误消息部分)。我可以得到的唯一错误消息
删除 xmlns 属性可解决此问题。
我可以使用xsl:element 来生成新的 XML,但这会导致 XSL 非常冗长且难以阅读。
所以我的问题是如何告诉 XSLT 不 在生成的 XML 中添加 xmls 属性?
提前致谢。
错误信息
以下是 ASP/IIS 提供给我们的错误消息:-
-
当配置文件的根元素(或任何其他元素)具有前缀定义
xmlns:con="http://schemas.microsoft.com/.NetConfiguration/v2.0"时,我会在 YSOD 中得到以下内容配置错误
说明:处理此请求所需的配置文件时出错。请查看下面的具体错误详细信息并适当地修改您的配置文件。
解析器错误消息:无法识别的属性“xmlns:con”。请注意,属性名称区分大小写。
-
当任何其他元素具有“xmlns”属性时,我会在 YSOD 中收到以下消息。
解析器错误消息:无法识别的属性“xmlns”。请注意,属性名称区分大小写。
1234563尽管启用了失败的请求跟踪,但我没有收到任何日志。不过这可能是 PEBKAC。
我的问题的解决方案
感谢 LarsH、Vincent Biragnet 和 Michael Kay 的回复,他们之间的回复解决了我的问题。
首先 - 正如迈克尔和文森特指出的那样,我发布的 XSLT 是错误的,sectionGroup 元素应该位于命名空间“http://schemas.microsoft.com/.NetConfiguration/v2. 0"。
为了对此进行排序,我在 XSLT 中定义了默认命名空间,如下所示 xmlns="http://schemas..."。
但是(为什么我不知道)sectionGroup 元素然后输出为
<sectionGroup name="elmah" xmlns:con="http://schemas.microsoft.com/.NetConfiguration/v2.0">
(xmlns:con 不是多余的吗?)。无论如何,配置解析器在 xmlns:con 存在时会出错。
但是 LarsH 来救场了 - 他使用 exclude-result-prefixes="msxsl con" 的建议摆脱了那些讨厌的事情。
所以我的(现在工作的)XSLT 的顶部现在看起来像这样
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:con="http://schemas.microsoft.com/.NetConfiguration/v2.0"
xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"
exclude-result-prefixes="msxsl con"
>
再次感谢大家的帮助
【问题讨论】:
-
您是否尝试将默认 xmlns 添加到您的 xsl:stylesheet 中?尝试添加
xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"。 -
+1 很好地解释了解决方案。
-
奇怪的是
<sectionGroup>应该在 .NetConfiguration 命名空间中;这意味着此处显示的配置 XML 无效:msdn.microsoft.com/en-us/library/aa479332.aspx -
是的 - 配置解析器似乎很乐意解析文件,无论是否将默认命名空间定义为“schemas.microsoft.com/.NetConfiguration/v2.0”。我刚刚意识到这对我不利 - 我刚刚编写的 XSLT 仅在元素位于 MSConfig 命名空间中时才有效。我将不得不对其进行处理,以便它可以在有或没有默认命名空间声明的情况下工作!
标签: xslt web-config elmah