1

在我的 program.wxs 文件中,我有这样的东西。

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="CLASSES">
            <Directory Id="dirAAAAA" Name="folderA">
                <Directory Id="dirBBBBB" Name="folderB">
                    <Component Id="cmpCCCCC" Guid="MY-GUID">
                        <File Id="filDDDDD" KeyPath="yes" Source="SourceDir\en_myfile.xml" />
                    </Component>
                </Directory>
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="LANG_PACKS_EN_US_COMPONENTS">
            <ComponentRef Id="cmpCCCCC" />
        </ComponentGroup>
    </Fragment>
</Wix>

我希望输出像

 <?xml version="1.0" encoding="utf-8"?>
    <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <Fragment>
            <DirectoryRef Id="CLASSES">
                <Directory Id="dirAAAAA" Name="folderA">
                    <Directory Id="dirBBBBB" Name="folderB">
                        <Component Id="en_cmpCCCCCC" Guid="MY-GUID">
                            <File Id="filDDDDD" KeyPath="yes" Source="SourceDir\en_myfile.xml" >
                                <CopyFile Id ="filDDDDD" DestinationProperty="dirBBBBB" DestinationName="myfile.xml/>
                            </File>
                        </Component>
                    </Directory>
                </Directory>
            </DirectoryRef>
        </Fragment>
        <Fragment>
            <ComponentGroup Id="LANG_PACKS_EN_US_COMPONENTS">
                <ComponentRef Id="en_cmpCCCCC" />
            </ComponentGroup>
        </Fragment>
    </Wix>

现在我有这个 .XSLT

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

  <xsl:template match="wix:Component/@Id">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="concat('en_', .)" />
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="wix:ComponentRef/@Id">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="concat('en_', .)" />
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="File">
      <xsl:apply-templates select="@* | node()" />
      <CopyFile Id="xxx" DestinationProperty="yyy" DestinationName="zzz"/>
  </xsl:template>

我想添加从节点和节点<CopyFile>获取值的节点<Directory><file Source>

你能帮我用 .XSLT 来改变这个 .wxs 吗?

4

2 回答 2

2

这应该这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wi="http://schemas.microsoft.com/wix/2006/wi" exclude-result-prefixes="wi">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="wi:Component/@Id | wi:ComponentRef/@Id">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="concat('en_', .)" />
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="wi:File">
    <xsl:copy>
      <xsl:variable name="leadingSpace" select="preceding-sibling::text()[1]" />
      <xsl:apply-templates select="@*"/>
      <!-- Add linebreak and indentation, as requested in the comments -->
      <xsl:value-of select="concat($leadingSpace, '  ')" />
      <CopyFile Id="{@Id}" DestinationProperty="{ancestor::wi:Directory[1]/@Id}" xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <xsl:attribute name="DestinationName">
          <xsl:call-template name="GetFileName">
            <xsl:with-param name="path" select="@Source"  />
          </xsl:call-template>
        </xsl:attribute>
      </CopyFile>
      <!-- Linebreak and indentation -->
      <xsl:value-of select="$leadingSpace"/>
    </xsl:copy>
  </xsl:template>

  <!-- Extracts a file name from a path, removing anything that has a \ after it. -->
  <xsl:template name="GetFileName">
    <xsl:param name="path" />
    <xsl:choose>
      <xsl:when test="contains($path, '\')">
        <xsl:call-template name="GetFileName">
          <xsl:with-param name="path" select="substring-after($path, '\')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="contains($path, '_')">
            <xsl:value-of select="substring-after($path, '_')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$path" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

在此输入上运行时:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <DirectoryRef Id="CLASSES">
      <Directory Id="dirAAAAA" Name="folderA">
        <Directory Id="dirBBBBB" Name="folderB">
          <Component Id="cmpCCCCC" Guid="MY-GUID">
            <File Id="filDDDDD" KeyPath="yes" Source="C:\SourceDirParent\SourceDir\en_myfile.xml" />
          </Component>
        </Directory>
      </Directory>
    </DirectoryRef>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="LANG_PACKS_EN_US_COMPONENTS">
      <ComponentRef Id="cmpCCCCC" />
    </ComponentGroup>
  </Fragment>
</Wix>

产生:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <DirectoryRef Id="CLASSES">
      <Directory Id="dirAAAAA" Name="folderA">
        <Directory Id="dirBBBBB" Name="folderB">
          <Component Id="en_cmpCCCCC" Guid="MY-GUID">
            <File Id="filDDDDD" KeyPath="yes" Source="C:\SourceDirParent\SourceDir\en_myfile.xml">
              <CopyFile Id="filDDDDD" DestinationProperty="dirBBBBB" DestinationName="myfile.xml" />
            </File>
          </Component>
        </Directory>
      </Directory>
    </DirectoryRef>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="LANG_PACKS_EN_US_COMPONENTS">
      <ComponentRef Id="en_cmpCCCCC" />
    </ComponentGroup>
  </Fragment>
</Wix>
于 2013-01-23T07:04:58.190 回答
0

你接近解决方案

这个更短更简单的转换

<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"
 xmlns="http://schemas.microsoft.com/wix/2006/wi">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="wix:Component">
   <Component Id="en_{@Id}">
    <xsl:apply-templates select="@*[not(name()='Id')]|node()"/>
   </Component>
  </xsl:template>

  <xsl:template match="wix:ComponentRef">
    <ComponentRef Id="en_{@Id}">
      <xsl:apply-templates select="@*[not(name()='Id')]|node()"/>
    </ComponentRef>
  </xsl:template>

  <xsl:template match="wix:File">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
      <CopyFile Id="xxx" DestinationProperty="yyy" DestinationName="zzz"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="CLASSES">
            <Directory Id="dirAAAAA" Name="folderA">
                <Directory Id="dirBBBBB" Name="folderB">
                    <Component Id="cmpCCCCC" Guid="MY-GUID">
                        <File Id="filDDDDD" KeyPath="yes" Source="SourceDir\en_myfile.xml" />
                    </Component>
                </Directory>
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="LANG_PACKS_EN_US_COMPONENTS">
            <ComponentRef Id="cmpCCCCC" />
        </ComponentGroup>
    </Fragment>
</Wix>

产生想要的正确结果:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="CLASSES">
            <Directory Id="dirAAAAA" Name="folderA">
                <Directory Id="dirBBBBB" Name="folderB">
                    <Component Id="en_cmpCCCCC" Guid="MY-GUID">
                        <File Id="filDDDDD" KeyPath="yes" Source="SourceDir\en_myfile.xml">
                            <CopyFile Id="xxx" DestinationProperty="yyy" DestinationName="zzz" />
                        </File>
                    </Component>
                </Directory>
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="LANG_PACKS_EN_US_COMPONENTS">
            <ComponentRef Id="en_cmpCCCCC"></ComponentRef>
        </ComponentGroup>
    </Fragment>
</Wix>
于 2013-01-23T14:22:33.100 回答