WiX XSLT 转换弄乱了我的格式
WiX XSLT transform messing up my formatting
我正在尝试使用转换将属性添加到组件描述中。它正在正确添加属性,但弄乱了我的 XML 格式。
我的目标是将属性 'Permanent="yes" ' 添加到组件。
XSLT 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:template match="wix:Component">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:attribute name="Permanent">
<xsl:text>yes</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="* | text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | text()">
<xsl:copy />
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
转换前的 .wxl 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="SystemFolder">
<Component Id="tdbgpp7.dll" Guid="{FA172CDA-D111-49BD-940F-F72EB8AC90DA}">
<File Id="tdbgpp7.dll" KeyPath="yes" Source="$(var.OC2.WinSys32)\tdbgpp7.dll" />
</Component>
</DirectoryRef>
</Fragment>
</Wix>
转换后如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="SystemFolder">
<Component Id="tdbgpp7.dll" Guid="{415E5416-AFE3-4658-8D74-489554345219}" Permanent="yes"><File Id="tdbgpp7.dll" KeyPath="yes" Source="$(var.OC2.WinSys32)\tdbgpp7.dll" /></Component>
</DirectoryRef>
</Fragment>
</Wix>
如您所见,它按预期添加了我的属性,但丢失了一些格式。该代码仍然有效,但失去了可读性。我知道我一定遗漏了一些简单的东西,但到目前为止我还没有明白。我是这个转换东西的菜鸟。
这是因为在匹配 wix:Component
的模板中,您执行了 <xsl:apply-templates select="*" />
。这意味着您仅将模板应用于元素,因此 wix:Component
内的文本节点(无关紧要的空白)将被剥离。
我建议 identity transform 并将模板应用于 wix:Component
模板中的 node()
...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="wix:Component">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:attribute name="Permanent">
<xsl:text>yes</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我正在尝试使用转换将属性添加到组件描述中。它正在正确添加属性,但弄乱了我的 XML 格式。
我的目标是将属性 'Permanent="yes" ' 添加到组件。
XSLT 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:template match="wix:Component">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:attribute name="Permanent">
<xsl:text>yes</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="* | text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | text()">
<xsl:copy />
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
转换前的 .wxl 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="SystemFolder">
<Component Id="tdbgpp7.dll" Guid="{FA172CDA-D111-49BD-940F-F72EB8AC90DA}">
<File Id="tdbgpp7.dll" KeyPath="yes" Source="$(var.OC2.WinSys32)\tdbgpp7.dll" />
</Component>
</DirectoryRef>
</Fragment>
</Wix>
转换后如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="SystemFolder">
<Component Id="tdbgpp7.dll" Guid="{415E5416-AFE3-4658-8D74-489554345219}" Permanent="yes"><File Id="tdbgpp7.dll" KeyPath="yes" Source="$(var.OC2.WinSys32)\tdbgpp7.dll" /></Component>
</DirectoryRef>
</Fragment>
</Wix>
如您所见,它按预期添加了我的属性,但丢失了一些格式。该代码仍然有效,但失去了可读性。我知道我一定遗漏了一些简单的东西,但到目前为止我还没有明白。我是这个转换东西的菜鸟。
这是因为在匹配 wix:Component
的模板中,您执行了 <xsl:apply-templates select="*" />
。这意味着您仅将模板应用于元素,因此 wix:Component
内的文本节点(无关紧要的空白)将被剥离。
我建议 identity transform 并将模板应用于 wix:Component
模板中的 node()
...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="wix:Component">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:attribute name="Permanent">
<xsl:text>yes</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>