为什么XSLT不像我的XPath查询?

时间:2011-11-02 15:31:51

标签: xml xslt xpath

我有一个XPath查询,它试图抓取特定文件节点的父节点。当我在Xselerator中使用XPath求值程序时,我的查询很好,但当我把它放入我的XSLT代码时,它给了我适合。这是我的XSLT代码:

<xsl:template match="//*[local-name()='Wix']/*[local-name()='Fragment'][1]/*[local-name()='DirectoryRef']/*[local-name()='Directory'][./@*[local-name()='Name'][.='bin']]/*[local-name()='Component']/*[local-name()='File'][./@*[local-name()='Source'][.='!(wix.SourceDeployDir)\bin\Client.exe']]/..">
<xsl:copy>
  <xsl:apply-templates select="@* | node()" />
  <xsl:element name="RemoveFolder" namespace="{namespace-uri()}">
    <xsl:attribute name="Id">DeleteShortcutFolder</xsl:attribute>
    <xsl:attribute name="Directory">DesktopFolder</xsl:attribute>
    <xsl:attribute name="On">uninstall</xsl:attribute>
  </xsl:element>
</xsl:copy>

有什么想法吗?

编辑:这是相关的XML(从较大的文件中清除):

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="INSTALLLOCATION">
<Directory Id="dirBD8892FBCC64DA5924D7F747259B8B87" Name="bin">
<Component Id="cmp92DC8F5323DA73C053179076052F92FF" Guid="{533500C1-ACB2-4A8D-866C-7CDB1DE75524}">
                    <File Id="fil7C1FC50442FC92D227AD1EDC1E6D259F" KeyPath="yes" Source="!(wix.SourceDeployDir)\bin\Client.exe">
                      <Shortcut Id="startmenuAdv" Directory="DesktopFolder" Advertise="yes" Name="!(wix.ProductName)" WorkingDirectory="INSTALLDIR" Icon="Icon.exe">
                        <Icon Id="Icon.exe" SourceFile="!(wix.SourceDeployDir)\Safeguard.SPI2.Client.exe" />
                      </Shortcut>
                      <netfx:NativeImage Id="ClientNativeImageId" Platform="64bit" Priority="0" AppBaseDirectory="INSTALLLOCATION" xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension" />
                    </File>
                </Component></Directory></DirectoryRef></Fragment></Wix>

我想要做的就是抓住Component节点。 Visual Studio给出了以下错误:在谓词外部的模式中只允许使用“子”和“属性”轴。 ...在\ Client.exe']] / - &gt; ..&lt; -

2 个答案:

答案 0 :(得分:4)

XSLT匹配模式不允许所有类型的XPath表达式,而是模式是XPath表达式的子集。您似乎想要访问父..,但不允许您在XSLT模式中执行此操作,除非它在谓词内。因此,您需要重写您的模式,而不是foo[predicate]/..使用*[foo[predicate]]

[编辑] 根据您的最新评论

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

<xsl:template xmlns:wi="http://schemas.microsoft.com/wix/2006/wi"
  match="wi:Component[wi:File[@Source[. = '!(wix.SourceDeployDir)\bin\Client.exe']]]">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <xsl:element name="RemoveFolder" namespace="{namespace-uri()}">
      <xsl:attribute name="Id">DeleteShortcutFolder</xsl:attribute>
      <xsl:attribute name="Directory">DesktopFolder</xsl:attribute>
      <xsl:attribute name="On">uninstall</xsl:attribute>
    </xsl:element>
  </xsl:copy>
</xsl:template>

可能就足够了(假设您要复制除添加元素的Component之外的所有内容。

答案 1 :(得分:1)

我认为最好在apply-templates指令中添加更多的选择逻辑来选择要处理的节点,而在模板规则中则更少。模板规则的匹配模式应该可以匹配规则可以合理地用于处理的所有元素,如果您不想处理所有这些元素,那么在apply-templates调用中选择您想要处理的元素

相关问题