草庐IT

关于xml:通过Xslt修改heat生成的WXS文件

codeneng 2023-03-28 原文

modify WXS file generated by heat through Xslt

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

1) 到达子元素(文件)

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

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

我的 xslt 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        <?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

1
2
3
4
5
6
7
8
9
10
11
12
<?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

1
2
3
4
5
6
7
8
9
10
11
12
<?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

1
2
3
4
5
6
7
8
9
10
11
12
<?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,使其格式正确。遍历到父元素的目的是什么?
  • 永久=yes 应该在组件标签而不是文件标签中定义
  • @abhinavpandey 你的编辑是什么意思? OP 希望将 permanent 属性添加到 File 元素,而不是 Component。你怎么比他/她更了解这件事?
  • 它是 wix,永久属性是文件标签的一部分 :)


试试这个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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"

将第二个模板更改为:

1
2
3
4
5
6
<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,您的输入中是否还有其他 Component 元素?
  • 我想在组件中添加属性,其对应的子项(文件元素)满足 source="SourceDir\\\\Debug\\\\settings.xml" 的条件
  • @ michael.hor257k ...Thankx 这是正确的,但问题是,它也在重新处理文件元素,这是不希望的。我需要原样的文件元素。

有关关于xml:通过Xslt修改heat生成的WXS文件的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  6. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  7. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  8. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  9. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  10. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

随机推荐