xml - 通过Xslt修改heat生成的WXS文件

标签 xml xslt wix

我正在通过xslt编辑directory.wxs中所有.config文件的组件。

1)到达子元素(文件)后我无法转到父元素(组件)

2)它覆盖所有属性而不是附加到原始属性。

我的 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:wix="http://schemas.microsoft.com/wix/2006/wi"
                    xmlns:my="my:my">

          <xsl:output method="xml" indent="no"/>

          <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/wix:Directory/wix:Component/wix:File[@Source="SourceDir\Debug\settings.xml"]'>
            <xsl:copy>
                    <xsl:apply-templates select=".." mode="backout"/>
                    <xsl:attribute name="Permanent">
                        <xsl:text>yes</xsl:text>
                    </xsl:attribute>
                </xsl:copy>
          </xsl:template>

        </xsl:stylesheet>

输入.wxs

<?xml version="1.0" encoding="utf-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <Fragment>
            <DirectoryRef Id="INSTALLFOLDER">
                <Directory Id="dirEF46C7404B50F3071431995F0F04741E" Name="bin">
                        <Component Id="cmp1B2A20D6C7B8EEB0F3E75C2D993AAC13" Guid="1346D980-EE76-4E6D-BA63-F1F0BB5A860D">
                            <File Id="fil46D415998FC8ECAB7106CB3185135601" KeyPath="yes" Source="SourceDir\Debug\settings.xml" />
                        </Component>
                    </Directory>
                </Directory>
            </DirectoryRef>
        </Fragment>

输出.xml

<?xml version="1.0" encoding="utf-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <Fragment>
            <DirectoryRef Id="INSTALLFOLDER">
                <Directory Id="dirEF46C7404B50F3071431995F0F04741E" Name="bin">
                        <Component Id="cmp1B2A20D6C7B8EEB0F3E75C2D993AAC13" Guid="1346D980-EE76-4E6D-BA63-F1F0BB5A860D">
                            <File Permanent="yes" />
                        </Component>
                    </Directory>
                </Directory>
            </DirectoryRef>
        </Fragment>

必需的输出.xml

<?xml version="1.0" encoding="utf-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <Fragment>
            <DirectoryRef Id="INSTALLFOLDER">
                <Directory Id="dirEF46C7404B50F3071431995F0F04741E" Name="bin">
                        <Component Id="cmp1B2A20D6C7B8EEB0F3E75C2D993AAC13" Guid="1346D980-EE76-4E6D-BA63-F1F0BB5A860D" Permanent="yes" >
                            <File Id="fil46D415998FC8ECAB7106CB3185135601" KeyPath="yes" Source="SourceDir\Debug\settings.xml" />
                        </Component>
                    </Directory>
                </Directory>
            </DirectoryRef>
        </Fragment>

最佳答案

尝试这样:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="wix:File[@Source='SourceDir\Debug\settings.xml']">
    <xsl:copy>
        <xsl:attribute name="Permanent">yes</xsl:attribute>
        <xsl:copy-of select="@*"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

I am unable to go to parent element(component) after reaching child element(file)

我认为没有必要这样做。如果您想修改父级,只需添加一个与 Component 匹配的模板,并让它到达子级之前执行即可。

另请注意,您的样式表尝试使用 mode="backout" 将模板应用到 File 的父级 - 但在这种模式下它不包含任何模板。

无论如何,您的预期输出显示父 Component 没有任何变化,因此这都是理论上的。


编辑:

回应您编辑的问题以及下面评论中的说明:

I want to add attribute in the component for which corresponding child (File elemeny) satisfy the condition that source="SourceDir\Debug\settings.xml"

将第二个模板更改为:

<xsl:template match="wix:Component[wix:File/@Source='SourceDir\Debug\settings.xml']">
    <xsl:copy>
        <xsl:attribute name="Permanent">yes</xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

关于xml - 通过Xslt修改heat生成的WXS文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31564578/

相关文章:

c# - WiX 自定义操作 - 构建失败

python - BeaitifulSoup 无法读取所有命名空间

android - 在 Android XML 中重用 TextView 代码

java - 对同一个 XSL 样式表使用不同的 HTML 模板

xml - 使用 xslt 重命名重复属性

java - 生成 .xsl 格式的二维码?

installation - NSIS与WiX与任何其他安装软件包

c# - WIX 如何从自定义操作访问源文件

java - (Java 12) 当库作为插件加载时 JAXB 不工作

java - fatal error :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException