基于属性

时间:2017-07-17 13:55:49

标签: xslt wix gac heat

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

以“

”结尾的@KeyPath附加内容
  • &#34;的.config&#34;应将@Assembly设置为&#34;否&#34;
  • &#34;的.dll&#34;应该有一个额外的属性<?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> ,其值为&#34; .net&#34;

以下是必须转换的XML I示例:

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

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

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

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

import json
results = []
# Your loop
foreach element in elements:
    element_json_object = ... # Construct a JSON object here
    results.append(element_json_object)

data = {
    "results": results
}

with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

1 个答案:

答案 0 :(得分: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>