【问题标题】:modify WXS file generated by heat through Xslt通过Xslt修改heat产生的WXS文件
【发布时间】:2015-10-12 09:55:56
【问题描述】:

我正在通过 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>

【问题讨论】:

  • 请更新您的 XML 以使其格式正确。遍历到父元素的目的是什么?
  • permanent = yes 应该定义在组件标签而不是文件标签中
  • @abhinavpandey 你的编辑是什么意思? OP 希望将permanent 属性添加到File 元素,而不是Component。你怎么比他/她更了解这个?
  • 它是wix,永久属性是文件标签的一部分:)

标签: xml xslt wix


【解决方案1】:

试试这个方法:

<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>

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

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

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

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


编辑:

针对您编辑的问题以及下方评论中的说明:

我想在对应的子组件中添加属性 (文件元素)满足条件 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>

【讨论】:

  • 但是没有关于我的组件的任何先前信息,因为我的 directory.wxs 文件是由 WIX heat 命令生成的。我所能做的就是通过它的子元素 File 来唯一标识该组件
  • @Keshav 恐怕我不明白你想说什么。您要修改File 还是Component?如果是Component,你的输入中会不会有other Component元素?
  • 我想在组件中添加属性,对应的子元素(文件元素)满足source="SourceDir\Debug\settings.xml"
  • @ michael.hor257k ...Thankx 这是正确的,但问题是,它也在重新处理文件元素,这是不希望的。我需要文件元素。
猜你喜欢
  • 2013-01-04
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多