【发布时间】:2017-07-17 13:55:48
【问题描述】:
我有一个使用 wix-toolset 中的 heat.exe 生成的 XML 文件。这将每个<File> 对象包装在<Component> 中。
我必须使用 XSLT v1.0 对其进行修改,以便将所有 <File> 中的 @Source 包含相同的文件名(无扩展名)提取到一个 <Component>。通常,@Source 仅以“.dll”或“.config”结尾。
附加的<File>s 结尾为:
- “.config”应该将
@KeyPath设置为“no” - “.dll”应该有一个额外的属性
@Assembly,其值为“.net”
这是一个需要转换的示例 XML I:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="GAC35">
<Component Id="cmp5BC59A7DCA65D1B974894AAA758DB693" Guid="{1A2AC82E-7AD9-4CB6-BF42-4D31FAD7786E}">
<File Id="filE7FFE881A5ECF045432F46FBA78AEDD4" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.config" />
</Component>
<Component Id="cmp7F80B805A2BDCF92241FB8019B91FF1C" Guid="{0F88D7E9-355A-40C1-AC8C-29BBB27690FB}">
<File Id="filB35F1D68CCC038864F21E76D8A9F5977" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.dll" />
</Component>
<Component Id="test1" Guid="{1A2AC82E-7AD9-4CB6-BF42-4D31FAD7786E}">
<File Id="filE7FFE881A5ECF045432F46FBA78AEDD4" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.config" />
</Component>
<Component Id="test12" Guid="{0F88D7E9-355A-40C1-AC8C-29BBB27690FB}">
<File Id="filB35F1D68CCC038864F21E76D8A9F5977" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.dll" />
</Component>
</DirectoryRef>
</Fragment>
<Fragment>
<ComponentGroup Id="HeatGenerated_Gac35Policies">
<ComponentRef Id="cmp5BC59A7DCA65D1B974894AAA758DB693" />
<ComponentRef Id="cmp7F80B805A2BDCF92241FB8019B91FF1C" />
<ComponentRef Id="test1" />
<ComponentRef Id="test2" />
</ComponentGroup>
</Fragment>
</Wix>
那么这应该是 XSL v1 的预期输出:
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="GAC35">
<Component Id="cmp5BC59A7DCA65D1B974894AAA758DB693"
Guid="{1A2AC82E-7AD9-4CB6-BF42-4D31FAD7786E}">
<File Id="filE7FFE881A5ECF045432F46FBA78AEDD4"
KeyPath="no"
Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.config"/>
<File Id="filB35F1D68CCC038864F21E76D8A9F5977"
KeyPath="yes"
Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.dll"
Assembly=".net"/>
</Component>
<Component Id="test1"
Guid="guid1">
<File Id="filE7FFE881A5ECF045432F46FBA78AEDD4"
KeyPath="no"
Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.config"/>
<File Id="filB35F1D68CCC038864F21E76D8A9F5977"
KeyPath="yes"
Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.dll"
Assembly=".net"/>
</Component>
</DirectoryRef>
</Fragment>
<Fragment>
<ComponentGroup Id="HeatGenerated_Gac35Policies">
<ComponentRef Id="cmp5BC59A7DCA65D1B974894AAA758DB693"/>
<ComponentRef Id="test1"/>
</ComponentGroup>
</Fragment>
</Wix>
编辑:这是我自己的最后一个“解决方案”(不完整),直到我使用了@michael.hor257k 的答案
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
xmlns="http://schemas.microsoft.com/wix/2006/wi">
<!-- Copy all attributes and elements to the output. -->
<xsl:output method="xml"
indent="yes"
omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="wix:Component">
<xsl:variable name="fileExtension">
<xsl:choose>
<xsl:when test="contains(wix:File/@Source, '.config')">
<xsl:value-of select="'.config'"/>
</xsl:when>
<xsl:when test="contains(wix:File/@Source, '.dll')">
<xsl:value-of select="'.dll'"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="precedingFileExtension">
<xsl:choose>
<xsl:when test="contains(preceding-sibling::wix:Component/wix:File/@Source, '.config')">
<xsl:value-of select="'.config'"/>
</xsl:when>
<xsl:when test="contains(preceding-sibling::wix:Component/wix:File/@Source, '.dll')">
<xsl:value-of select="'.dll'"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:apply-templates select="wix:File[(substring-before(substring-after(@Source,'\'), $fileExtension) = substring-before(substring-after(preceding::wix:Component/wix:File/@Source,'\'), $precedingFileExtension))]" mode="files"/>
</xsl:template>
<xsl:template match="wix:File" mode="files">
<xsl:variable name="fileExtension">
<xsl:choose>
<xsl:when test="contains(@Source, '.config')">
<xsl:value-of select="'.config'"/>
</xsl:when>
<xsl:when test="contains(@Source, '.dll')">
<xsl:value-of select="'.dll'"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="otherFileExtension">
<xsl:choose>
<xsl:when test="contains(@Source, '.config')">
<xsl:value-of select="'.dll'"/>
</xsl:when>
<xsl:when test="contains(@Source, '.dll')">
<xsl:value-of select="'.config'"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<Component>
<xsl:copy-of select="parent::wix:Component/@*" />
<xsl:copy>
<xsl:copy-of select="@*" />
</xsl:copy>
<xsl:variable name="source" select="substring-before(substring-after(@Source,'\'), $fileExtension)"/>
<xsl:apply-templates select="//wix:File[substring-before(substring-after(@Source,'\'), $otherFileExtension)=$source]" />
</Component>
</xsl:template>
<xsl:key name="policy-config-file"
match="wix:File[contains(@Source, '.config')]"
use="@Id" />
<xsl:template match="wix:File[key('policy-config-file', @Id)]" >
<xsl:element name="File">
<xsl:apply-templates select="@*" />
<xsl:attribute name="KeyPath">
<xsl:value-of select="'no'"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
这很难理解。请用文字解释问题,可能将示例减少到演示问题所需的最低限度。 --附言
*[self::wix:File]到底有什么意义?这与wix:File有何不同? -
我需要一个 XSL 文件,在该文件的输出中,所有
对象在同一个 下具有相同的文件名(无扩展名)。我也尝试使用 @Source 的子字符串进行 Muenchian 分组,但没有成功。 -
好吧,那你为什么不把这个例子简化一下呢? -- .提示:您必须分两步执行此操作:(1)提取文件名; (2) 孟池分组。您将使用哪种 XSLT 处理器?您可以在此处使用扩展功能的一些帮助。
-
我在 Visual Studio 中使用它,这应该使用 XSLT 处理器 v1。我尝试仅使用子字符串方法提取文件名,但由于它们必须是动态的,因此我无法再使用分组所需的 key() 函数。我已经用
标记在末尾动态切断了扩展名,您能否提供一个有效的 XSLT 来提取文件名并将它们分组?