【问题标题】:Merge nodes with XSL v.1.0 based on a substring of an Attribute基于属性的子字符串使用 XSL v.1.0 合并节点
【发布时间】:2017-07-17 13:55:48
【问题描述】:

我有一个使用 wix-toolset 中的 heat.exe 生成的 XML 文件。这将每个<File> 对象包装在<Component> 中。 我必须使用 XSLT v1.0 对其进行修改,以便将所有 <File> 中的 @Source 包含相同的文件名(无扩展名)提取到一个 <Component>。通常,@Source 仅以“.dll”或“.config”结尾。

附加的<File>s 结尾为:

  • “.config”应该将@KeyPath设置为“no”
  • “.dll”应该有一个额外的属性@Assembly,其值为“.net”

这是一个需要转换的示例 XML I:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
    <DirectoryRef Id="GAC35">
        <Component Id="cmp5BC59A7DCA65D1B974894AAA758DB693" Guid="{1A2AC82E-7AD9-4CB6-BF42-4D31FAD7786E}">
            <File Id="filE7FFE881A5ECF045432F46FBA78AEDD4" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.config" />
        </Component>
        <Component Id="cmp7F80B805A2BDCF92241FB8019B91FF1C" Guid="{0F88D7E9-355A-40C1-AC8C-29BBB27690FB}">
            <File Id="filB35F1D68CCC038864F21E76D8A9F5977" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.dll" />
        </Component>
        <Component Id="test1" Guid="{1A2AC82E-7AD9-4CB6-BF42-4D31FAD7786E}">
            <File Id="filE7FFE881A5ECF045432F46FBA78AEDD4" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.config" />
        </Component>
        <Component Id="test12" Guid="{0F88D7E9-355A-40C1-AC8C-29BBB27690FB}">
            <File Id="filB35F1D68CCC038864F21E76D8A9F5977" KeyPath="yes" Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.dll" />
        </Component>
    </DirectoryRef>
</Fragment>
<Fragment>
    <ComponentGroup Id="HeatGenerated_Gac35Policies">
        <ComponentRef Id="cmp5BC59A7DCA65D1B974894AAA758DB693" />
        <ComponentRef Id="cmp7F80B805A2BDCF92241FB8019B91FF1C" />
        <ComponentRef Id="test1" />
        <ComponentRef Id="test2" />
    </ComponentGroup>
</Fragment>
</Wix>

那么这应该是 XSL v1 的预期输出:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
  <DirectoryRef Id="GAC35">
     <Component Id="cmp5BC59A7DCA65D1B974894AAA758DB693"
                Guid="{1A2AC82E-7AD9-4CB6-BF42-4D31FAD7786E}">
        <File Id="filE7FFE881A5ECF045432F46FBA78AEDD4"
              KeyPath="no"
              Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.config"/>
        <File Id="filB35F1D68CCC038864F21E76D8A9F5977"
              KeyPath="yes"
              Source="$(var.HarvestLoggingGac35Policy)\Policy.1.0.Logging.dll"
              Assembly=".net"/>
     </Component>
     <Component Id="test1"
                Guid="guid1">
        <File Id="filE7FFE881A5ECF045432F46FBA78AEDD4"
              KeyPath="no"
              Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.config"/>
        <File Id="filB35F1D68CCC038864F21E76D8A9F5977"
              KeyPath="yes"
              Source="$(var.HarvestLoggingGac35Policy)\Policy.2.0.Logging.dll"
              Assembly=".net"/>
     </Component>
  </DirectoryRef>
</Fragment>
<Fragment>
  <ComponentGroup Id="HeatGenerated_Gac35Policies">
     <ComponentRef Id="cmp5BC59A7DCA65D1B974894AAA758DB693"/>
     <ComponentRef Id="test1"/>
  </ComponentGroup>
</Fragment>
</Wix>

编辑:这是我自己的最后一个“解决方案”(不完整),直到我使用了@michael.hor257k 的答案

<?xml version="1.0" ?>
 <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
            xmlns="http://schemas.microsoft.com/wix/2006/wi">

<!-- Copy all attributes and elements to the output. -->
<xsl:output method="xml"
          indent="yes"
          omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

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


<xsl:template match="wix:Component">

<xsl:variable name="fileExtension">
  <xsl:choose>
    <xsl:when test="contains(wix:File/@Source, '.config')">
      <xsl:value-of select="'.config'"/>
    </xsl:when>
    <xsl:when test="contains(wix:File/@Source, '.dll')">
      <xsl:value-of select="'.dll'"/>
    </xsl:when>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="precedingFileExtension">
  <xsl:choose>
    <xsl:when test="contains(preceding-sibling::wix:Component/wix:File/@Source, '.config')">
      <xsl:value-of select="'.config'"/>
    </xsl:when>
    <xsl:when test="contains(preceding-sibling::wix:Component/wix:File/@Source, '.dll')">
      <xsl:value-of select="'.dll'"/>
    </xsl:when>
  </xsl:choose>
</xsl:variable>

<xsl:apply-templates select="wix:File[(substring-before(substring-after(@Source,'\'), $fileExtension) = substring-before(substring-after(preceding::wix:Component/wix:File/@Source,'\'), $precedingFileExtension))]" mode="files"/>
</xsl:template>


<xsl:template match="wix:File" mode="files">

<xsl:variable name="fileExtension">
  <xsl:choose>
    <xsl:when test="contains(@Source, '.config')">
      <xsl:value-of select="'.config'"/>
    </xsl:when>
    <xsl:when test="contains(@Source, '.dll')">
      <xsl:value-of select="'.dll'"/>
    </xsl:when>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="otherFileExtension">
  <xsl:choose>
    <xsl:when test="contains(@Source, '.config')">
      <xsl:value-of select="'.dll'"/>
    </xsl:when>
    <xsl:when test="contains(@Source, '.dll')">
      <xsl:value-of select="'.config'"/>
    </xsl:when>
  </xsl:choose>
</xsl:variable>

<Component>
  <xsl:copy-of select="parent::wix:Component/@*" />
  <xsl:copy>
    <xsl:copy-of select="@*" />
  </xsl:copy>
  <xsl:variable name="source" select="substring-before(substring-after(@Source,'\'), $fileExtension)"/>

  <xsl:apply-templates select="//wix:File[substring-before(substring-after(@Source,'\'), $otherFileExtension)=$source]" />
</Component>
</xsl:template>



<xsl:key name="policy-config-file"
         match="wix:File[contains(@Source, '.config')]"
         use="@Id" />

<xsl:template match="wix:File[key('policy-config-file', @Id)]" >
  <xsl:element name="File">
    <xsl:apply-templates select="@*" />
    <xsl:attribute name="KeyPath">
      <xsl:value-of select="'no'"/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

【问题讨论】:

  • 这很难理解。请用文字解释问题,可能将示例减少到演示问题所需的最低限度。 --附言*[self::wix:File] 到底有什么意义?这与wix:File 有何不同?
  • 我需要一个 XSL 文件,在该文件的输出中,所有 对象在同一个 下具有相同的文件名(无扩展名)。我也尝试使用 @Source 的子字符串进行 Muenchian 分组,但没有成功。
  • 好吧,那你为什么不把这个例子简化一下呢? -- .提示:您必须分两步执行此操作:(1)提取文件名; (2) 孟池分组。您将使用哪种 XSLT 处理器?您可以在此处使用扩展功能的一些帮助。
  • 我在 Visual Studio 中使用它,这应该使用 XSLT 处理器 v1。我尝试仅使用子字符串方法提取文件名,但由于它们必须是动态的,因此我无法再使用分组所需的 key() 函数。我已经用 标记在末尾动态切断了扩展名,您能否提供一个有效的 XSLT 来提取文件名并将它们分组?

标签: xslt wix gac heat


【解决方案1】:

您不能按节点没有的东西对节点进行分组。如果您想通过不带扩展名的文件名对组件进行分组,则必须首先将此类值添加到每个组件 - 作为元素或作为属性。这是因为在 XSLT 1.0 中删除扩展并不简单,并且不能通过单个 XPath 表达式来完成。

完成此操作后,您可以继续并将 Muenchian 分组应用于中间结果:

XSLT 1.0

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

<xsl:key name="k" match="wix:Component" use="key"/>

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

<xsl:template match="wix:DirectoryRef">
    <!-- first pass -->
    <xsl:variable name="components">
        <xsl:for-each select="wix:Component">
            <xsl:copy>
                <xsl:apply-templates select="@* | *"/>
                <key>
                    <xsl:call-template name="remove-last-token">
                        <xsl:with-param name="text" select="wix:File/@Source"/>
                        <xsl:with-param name="delimiter" select="'.'"/>
                    </xsl:call-template>
                </key>
            </xsl:copy>
        </xsl:for-each>
    </xsl:variable>
    <!-- output -->
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="exsl:node-set($components)/wix:Component[count(. | key('k', key)[1]) = 1]">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:copy-of select="key('k', key)/wix:File"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<xsl:template name="remove-last-token">
    <xsl:param name="text"/>
    <xsl:param name="delimiter"/>
    <xsl:value-of select="substring-before($text, $delimiter)"/>
    <xsl:if test="contains(substring-after($text, $delimiter), $delimiter)">
        <xsl:value-of select="$delimiter"/>
        <xsl:call-template name="remove-last-token">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            <xsl:with-param name="delimiter" select="$delimiter"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

演示:http://xsltransform.net/93dEHG9


附录:

如果您可以放心地假设扩展名始终为.dll.config,那么您可以将其简化为:

XSLT 1.0

<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="*"/>

<xsl:key name="k" match="wix:Component" use="substring-before(concat(substring-before(concat(wix:File/@Source, '.dll'), '.dll'), '.config'), '.config')"/>

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

<xsl:template match="wix:DirectoryRef">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:for-each select="wix:Component[count(. | key('k', substring-before(concat(substring-before(concat(wix:File/@Source, '.dll'), '.dll'), '.config'), '.config'))[1]) = 1]">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:copy-of select="key('k', substring-before(concat(substring-before(concat(wix:File/@Source, '.dll'), '.dll'), '.config'), '.config'))/wix:File"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2011-05-29
    相关资源
    最近更新 更多