基于属性的子字符串使用 XSL v.1.0 合并节点
Merge nodes with XSL v.1.0 based on a substring of an Attribute
我有一个 XML 文件是用 heat.exe 从 wix-toolset 生成的。它将每个 <File>
对象包装在 <Component>
中。
我必须使用 XSLT v1.0 对此进行修改,以便 @Source
包含相同文件名(无扩展名)的所有 <File>
都应提取到一个 <Component>
。通常,@Source
仅以“.dll”或“.config”结尾。
另外 <File>
结尾为:
- ".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>
编辑:这是我自己的最后一个"solution"(不完整),直到我使用@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>
您不能根据节点没有的东西对节点进行分组。如果您想按不带扩展名的文件名对组件进行分组,则必须首先向每个组件添加这样的值 - 作为元素或属性。这是因为删除扩展名在 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>
我有一个 XML 文件是用 heat.exe 从 wix-toolset 生成的。它将每个 <File>
对象包装在 <Component>
中。
我必须使用 XSLT v1.0 对此进行修改,以便 @Source
包含相同文件名(无扩展名)的所有 <File>
都应提取到一个 <Component>
。通常,@Source
仅以“.dll”或“.config”结尾。
另外 <File>
结尾为:
- ".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>
编辑:这是我自己的最后一个"solution"(不完整),直到我使用@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>
您不能根据节点没有的东西对节点进行分组。如果您想按不带扩展名的文件名对组件进行分组,则必须首先向每个组件添加这样的值 - 作为元素或属性。这是因为删除扩展名在 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>