WiX Toolset Heat工具从源目录中收集组件后,它会生成一个wxs文件,例如以下内容。
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="TARGETDIR">
<Directory Id="dirGeneratedID1" Name="MySourceDirName">
<Component Id="cmpGeneratedID1" Guid="*">
<File Id="filGeneratedID1" KeyPath="yes" Source="$(var.SourceRootDir)\File1.dll" />
</Component>
<Component Id="cmpGeneratedID2" Guid="*">
<File Id="filGeneratedID2" KeyPath="yes" Source="$(var.SourceRootDir)\File2.dll" />
</Component>
<Directory Id="dirGeneratedID2" Name="MyNestedDirName">
<Component Id="cmpGeneratedID3" Guid="*">
<File Id="filGeneratedID3" KeyPath="yes" Source="$(var.SourceRootDir)\MyNestedDirName\File3.dll" />
</Component>
</Directory>
</Directory>
</DirectoryRef>
</Fragment>
</Wix>
我的任务是仅更改父目录元素的Name属性的值。那就是将/ Wix / Fragment / DirectoryRef / Directory [@ Name ='MySourceDirName']元素的Name属性值从'MySourceDirName'更改为'MY_RENAMED_SOURCE_DIR_NAME',因此得到以下内容。
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="TARGETDIR">
<Directory Id="dirGeneratedID1" Name="MY_RENAMED_SOURCE_DIR_NAME">
<Component Id="cmpGeneratedID1" Guid="*">
<File Id="filGeneratedID1" KeyPath="yes" Source="$(var.SourceRootDir)\File1.dll" />
</Component>
<Component Id="cmpGeneratedID2" Guid="*">
<File Id="filGeneratedID2" KeyPath="yes" Source="$(var.SourceRootDir)\File2.dll" />
</Component>
<Directory Id="dirGeneratedID2" Name="MyNestedDirName">
<Component Id="cmpGeneratedID3" Guid="*">
<File Id="filGeneratedID3" KeyPath="yes" Source="$(var.SourceRootDir)\MyNestedDirName\File3.dll" />
</Component>
</Directory>
</Directory>
</DirectoryRef>
</Fragment>
</Wix>
由于是Heat工具生成了wxs文件,并且源目录名称为'MySourceDirName',所以目录元素的Name属性已生成为等于'MySourceDirName',以后需要XSL将其转换为我想要的新文件夹名称' MY_RENAMED_SOURCE_DIR_NAME'。
我使用下面的XSLT,但是它不起作用,并且出现错误“ DirectoryRef元素包含意外的属性'Name'。”
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
exclude-result-prefixes="wix">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/wix:Wix/wix:Fragment/wix:DirectoryRef/wix:Directory">
<xsl:attribute name="Name">
<xsl:choose>
<xsl:when test=". = 'MySourceDirName'">
<xsl:text>MY_RENAMED_SOURCE_DIR_NAME</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet
并且XSLT之后生成的wxs文件变成错误且不完整的文件:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="SciemetricDIR" Name="" />
</Fragment>
</Wix>
如果更改我的XSLT并添加xsl:copy以按以下方式还原内容(此处仅显示整个XSLT内容的影响规则)
<xsl:template match="/wix:Wix/wix:Fragment/wix:DirectoryRef/wix:Directory">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
<xsl:attribute name="Name">
<xsl:choose>
<xsl:when test=". = 'MySourceDirName'">
<xsl:text>MY_RENAMED_SOURCE_DIR_NAME</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
然后我得到这个错误:“将转换C:\ path-to-my-transform-file \ massageAfterHeatHarvesting.xsl应用于收集的WiX时出错:在文本,注释,pi之后,无法将属性和名称空间节点添加到父元素,或已经添加了子元素节点。MySoulutionName heat.exe 0“
我的XSLT有什么问题?如果我明确将目标子元素放在其下方,为什么它会触及DirectoryRef父元素?非常感谢任何支持。预先感谢。
XSLT处理器遇到Directory元素,然后按照指定的方式创建Name属性,而没有其他任何事情(它停止了。)>
尝试更改
<xsl:template match="/wix:Wix/wix:Fragment/wix:DirectoryRef/wix:Directory">
到
<xsl:template match="/wix:Wix/wix:Fragment/wix:DirectoryRef/wix:Directory/@Name">
实际上,将条件作为谓词移动到match属性更好:
<xsl:template match="/wix:Wix/wix:Fragment/wix:DirectoryRef/wix:Directory/@Name[.='MySourceDirName']">
<xsl:attribute name="Name">MY_RENAMED_SOURCE_DIR_NAME</xsl:attribute>
</xsl:template>