为什么 XSLT 不喜欢我的 XPath 查询?

Iro*_*n84 1 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>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:这是相关的 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>
Run Code Online (Sandbox Code Playgroud)

我想要做的就是获取 Component 节点。Visual Studio 给了我以下错误:谓词之外的模式中只允许使用“子”和“属性”轴。...in\Client.exe']]/ ​​-->..<--

Mar*_*nen 5

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>
Run Code Online (Sandbox Code Playgroud)

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